how to code discord bot in 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: 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']
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 4: Discord bot source code Python
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
Example 5: discord python application bot
q_list = [
'What is your favorite color?',
'Is the Sky Blue?',
'Am I the best bot ever?'
]
a_list = []
@client.command(aliases=['staff-application'])
async def staff_application(ctx):
a_list = []
submit_channel = client.get_channel(<your channel id>)
channel = await ctx.author.create_dm()
def check(m):
return m.content is not None and m.channel == channel
for question in q_list:
sleep(.5)
await channel.send(question)
msg = await client.wait_for('message', check=check)
a_list.append(msg.content)
submit_wait = True
while submit_wait:
await channel.send('End of questions - "submit" to finish')
msg = await client.wait_for('message', check=check)
if "submit" in msg.content.lower():
submit_wait = False
answers = "\n".join(f'{a}. {b}' for a, b in enumerate(a_list, 1))
submit_msg = f'Application from {msg.author} \nThe answers are:\n{answers}'
await submit_channel.send(submit_msg)