Reputation: 21
It only displays one of the commands at one given time.
If I write !hi
or !bye
it won't work, but if I write !sing
it would output la la la
.
If I switch the character before it, it would change to
!hi
or !sing
not working
but !bye
working and saying Goodbye!
import os
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi'):
await message.channel.send('Hello!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!bye'):
await message.channel.send('Goodbye Friend!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!sing'):
await message.channel.send('la la la')
client.run(os.getenv('TOKEN'))```
Upvotes: 2
Views: 694
Reputation: 6944
This is a perfect example of using commands.Bot()
:
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
@bot.command()
async def hi(ctx):
await ctx.send("Hello!")
@bot.command()
async def sing(ctx):
await ctx.send("la la la!")
@bot.command()
async def bye(ctx):
await ctx.send("Goodbye friend!")
Bot()
inherits from Client()
, and provides much more functionality when it comes to handling commands!
Upvotes: 1
Reputation: 957
Only have one event, don't make a new one for every command.
@client.event
async def on_message(message):
if message.author == client.user:
return
elif message.content.startswith('!sing'):
await message.channel.send('La La la.')
elif message.content.startswith('!hi'):
await message.channel.send('Hello!')
Upvotes: 2
Reputation: 388
Don't repeat on_message events. Have only one and put if statements into this one event.
Upvotes: 1
Reputation: 80
You're trying to use multiple events, instead use all of the events in one event like this:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi'):
await message.channel.send('Hello!')
elif message.content.startswith('!bye'):
await message.channel.send('Goodbye Friend!')
elif message.content.startswith('!sing'):
await message.channel.send('la la la')
Also, make sure to have elif
in the other events instead of if
.
This should do it.
Upvotes: 2