Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

bots - Sending a message when prefix was send discord.py

I was making a discord bot with the prefix "!c", but I wanted that if people sent "!c" that an embed showed up so I fixed it by doing this:

client = commands.Bot(command_prefix='!')
client.remove_command("help")

@client.group()
async def c(ctx):
    YourEmbedCodeHere

I want to add a command "!c help" that it sends the same embed. I tried it by doing:

@c.group(invoke_without_command= True)
async def help(ctx):
   embed = discord.Embed(title="ChiBot", description="ChiBot by Chita! A very usefull moderation, level, invite and misc bot!", color=0x62e3f9)
   embed.add_field(name="Commands:", value="Do !help (command) to get help on that command!", inline=False)
   embed.add_field(name="Misc", value="!c help    !c randomimage    !c invite    !c echo", inline=False)
   embed.add_field(name="Moderation", value="!c ban    !c kick    !c warn    !c mute    !c unmute    !c warns      !c warnings", inline=False)
   embed.add_field(name="Levels", value="!c rank    !c dashboard   ", inline=False)
   embed.add_field(name="Invites", value="!c invites (help with setting it up)", inline=False)
   embed.set_footer(text="Created by Chita#8005")
   await ctx.send(embed=embed)

But when I try that, it sends double because "!c" is still a command for that same embed. How can I make it so it only sends 1 embed? And I want to add other "!c" commands as well so I need a solution to remove the !c embed if there is something behind the !c

Regards, Chita

question from:https://stackoverflow.com/questions/65938520/sending-a-message-when-prefix-was-send-discord-py

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

1 Reply

0 votes
by (71.8m points)

You can use the on_message event listener to check for that.

@client.listen('on_message')
async def foo(message):
   await client.wait_until_ready()
   ctx = await client.get_context(message)
   if message.content == '!c':
       await ctx.send(embed=embed) # send your embed here
       return #makes sure that we dont reply twice to `!c`

we are using client.wait_until_ready() so that the client doesn't respond to messages before it is connected.

Optionally, you can add

if message.author.bot:
    return

to make sure that we don't reply to bots.

Resources: on_message wait_until_ready


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...