Flameinfiren
Flameinfiren

Reputation: 1

How do i put more than one command leading to the same response in discord.py?

@client.event
async def on_message(message):
    if message.content == "fase":
        channel = (mychannel)
        await message.channel.send("Fase 1")

I'm trying to make the message.content detect multiple words and send the same message.channel.send I tried

if message.content.lower() == "fase", "estagio", "missao", "sala":
if message.content.lower() == ("fase", "estagio", "missao", "sala"):
if message.content.lower() == "fase" or  "estagio" or "missao" or "sala":
if message.content.lower() == ("fase" or  "estagio" or "missao" or "sala"):

I read this post: How do I allow for multiple possible responses in a discord.py command?

That is the same exact problem but in his case it was the CaseSensitiveProblem that i already fixed in my code

And the second code for multiple words was:

bot = commands.Bot(command_prefix='!', case_insensitive=True)
@bot.command(aliases=['info', 'stats', 'status'])
    async def about(self):
        # your code here

And i did it and got a lot of errors that made the bot not even run (im using PyCharm with discord.py 1.4.1 and python 3.6):

#import and token things up here
bot = commands.Bot(command_prefix='i.')
@bot.command(aliases=['fase', 'estagio', 'missao', 'sala']) #'@' or 'def' expected
    async def flame(self): #Unexpected indent // Unresolved reference 'self'
        if message.content(self): #Unresolved reference 'message'
            await message.send("Fase 1") #Unresolved reference 'message' // Statement expected, found Py:DEDENT


What can i do to fix it?

Upvotes: 0

Views: 2622

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

Here's how to use the Commands extension:

from discord.ext import commands

bot = commands.Bot(command_prefix='!', case_insensitive=True)
@bot.command(aliases=['info', 'stats', 'status'])
async def about(ctx):
    #Your code here

Every commands have the following in common:

  • They are created using the bot.command() decorator.
  • By default, the command name is the function name.
  • The decorator and the function definition must have the same indentation level.
  • ctx (the fist argument) will be a discord.Context object, which contains a lot of informations (message author, channel, and content, discord server, the command used, the aliase the command was invoked with, ...)

Then, ctx allows you to use some shortcuts:

  • message.channel.send() becomes ctx.send()
  • message.author becomes ctx.author
  • message.channel becomes ctx.channel

Command arguments are also easier to use:

from discord import Member
from discord.ext import commands

bot = commands.Bot(command_prefix='!', case_insensitive=True)

#Command call example: !hello @Mr_Spaar
#Discord.py will transform the mention to a discord.Member object
@bot.command()
async def hello(ctx, member: Member):
    await ctx.send(f'{ctx.author.mention} says hello to {member.mention}')

#Command call example: !announce A new version of my bot is available!
#"content" will contain everything after !announce (as a single string)
@bot.command()
async def announce(ctx, *, content):
    await ctx.send(f'Announce from {ctx.author.mention}: \n{content}')

#Command call example: !sum 1 2 3 4 5 6
#"numbers" will contain everything after !sum (as a list of strings)
@bot.command()
async def sum(ctx, *numbers):
    numbers = [int(x) for x in numbers]
    await ctx.send(f'Result: {sum(numbers)}')

Upvotes: 3

Related Questions