Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

python - Is there a way to get the name of the command from the message (Discord.py)

Usually when you create a command with the discord.py module, you do something like this:

@commands.command(name="my_command", pass_context=True, aliases=["mc"])
def my_command(ctx):
    #Do some command action here

I want to be able to use my Discord bot to moderate commands so that commands can only be used in designated channels, e.g. "my_command" can only be used in #general.

One easy way to get the command name would be to do something like this:

def get_command_name(message, prefix):
    command = message.split()[0].strip(prefix)

It would be nicer if I could used something like message.command_used to get the command, but I checked the documentation and there doesn't seem to be anything like that. Is there a way to get the command used from context rather than getting the command from string manipulation?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

but I checked the documentation and there doesn't seem to be anything like that

There is, in the docs for commands.Context you can find command and invoked_with, both of which you can use to get the name of the command. In case you want to include possible subcommands, there's invoked_subcommand as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...