Here are some examples of Discord bot code samples. Whether you're looking to create a simple greeting bot or a complex moderation bot, these examples can help you get started.
Basic Greeting Bot
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello there!')
bot.run('YOUR_BOT_TOKEN')
Moderation Bot
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('!kick'):
await message.channel.send(f'Kicking {message.mention}')
bot.run('YOUR_BOT_TOKEN')
Interactive Command Bot
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
@bot.command()
async def ping(ctx):
await ctx.send('Pong!')
bot.run('YOUR_BOT_TOKEN')
For more advanced bots, check out our advanced Discord bot tutorials.
[center]