Discord.py bot example
Example 1: discord.py simple 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 2: 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 3: 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 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']
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
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='prefix here')
@bot.event
async def on_ready():
print('Logged in as: ' + bot.user.name)
print('Ready!\n')
@bot.command()
async def commandname(ctx, *, somevariable)
Code goes here
await ctx.send('Message')
bot.run('yourtoken')
Example 6: How to setup discord.py bot
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='!')
token = 'YOUR TOKEN HERE'
@client.command()
async def hello(ctx):
await ctx.send('Hello I am a Test Bot!')
@client.event
async def on_ready():
print('Hello, I am now running')
client.run(token)