how to create a discord bot with python 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: how to make a discord bot python
# pip install discord
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):
# don't respond to ourselves
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 4: python discord bot moderate chat
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
word_list = ['cheat', 'cheats', 'hack', 'hacks', 'internal', 'external', 'ddos', 'denial of service']
# don't respond to ourselves
if message.author == self.user:
return
messageContent = message.content
if len(messageContent) > 0:
for word in word_list:
if word in messageContent:
await message.delete()
await message.channel.send('Do not say that!')
messageattachments = message.attachments
if len(messageattachments) > 0:
for attachment in messageattachments:
if attachment.filename.endswith(".dll"):
await message.delete()
await message.channel.send("No DLL's allowed!")
elif attachment.filename.endswith('.exe'):
await message.delete()
await message.channel.send("No EXE's allowed!")
else:
break
client = MyClient()
client.run('token here')
Example 5: Discord.py bot example
#Anything commented out is optional
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='prefix here')
@bot.event
async def on_ready():
# await bot.change_presence(activity=discord.Game(name="Rich Presence Here"))
print('Logged in as: ' + bot.user.name)
print('Ready!\n')
@bot.command()
async def commandname(ctx, *, somevariable)
#If you don't need a variable, then you only need (ctx)
# """Command description"""
Code goes here
await ctx.send('Message')
bot.run('yourtoken')