python discord bot code example
Example 1: bot discord python
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!", description="The description")
@bot.event
async def on_ready():
print("Ready !")
@bot.command()
async def ping(ctx):
await ctx.send('**pong**')
bot.run("enter the token here between the quotes")
Example 2: how to import discord in python
py -3 -m pip install -U discord.py
Example 3: discord.py bot
import discord
from discord.ext import commands
client = commands.Bot(comand_prefix='bot prefix here')
@client.event()
async def on_ready():
print(f"Bot online and logged in as {self.client.user}")
@client.command(aliases=["ms", "aliases!"])
async def ping(ctx, a_variable):
await ctx.send(f"Pong! {round(client.latency * 1000)}ms. Your input was {a_variable}")
client.run('your token here')
Example 4: how to make a discord bot python
import discord
class MyClient(discord.Client):
async def on_connect(self):
print('[LOGS] Connecting to discord!')
async def on_ready(self):
print('[LOGS] Bot is ready!')
print("""[LOGS] Logged in: {}\n[LOGS] ID: {}\n[LOGS] Number of users: {}""".format(self.bot.user.name, self.bot.user.id, len(set(self.bot.get_all_members()))))
await self.bot.change_presence(activity=discord.Game(name="Weeke is a god!"))
async def on_resumed(self):
print("\n[LOGS] Bot has resumed session!")
async def on_message(self, message):
if message.author == self.user:
return
if message.content == 'ping':
await ctx.send(f'Client Latency: {round(self.bot.latency * 1000)}')
client = MyClient()
client.run('token')
Example 5: discordpy base code
import discord
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('your token here')
Example 6: discord bot python
import discord.py
client = discord.Client()
@client.event
async def on_ready():
print("Logged in as {0.user}".format(client)
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '.hello':
print("Hello there!!")
client.run('here put your token')