简体   繁体   中英

How to make python discord bot execute/use a command

This is what I have right now and it does successfully sends the message every 60 seconds.

@tasks.loop(seconds=60)
async def mytask():
    channel = bot.get_channel(305347032569348107)
    await channel.send('Example message')

However when I change await channel.send('Example message') to await channel.send('!coin') it just sends the.coin message instead of executing/using the !coin command.

and I also tried this await channel.send(!coin) but this doesn't even run and shows SyntaxError: invalid syntax error.

Simply, keep in mind that your bot is only responsible for sending !coin to whichever channel. It is up to the bot that actually handles the !coin command to decide if it wants to run whatever logic is associated with the !coin command and respond or not.
Most discord bots will not even consider "commands" from another bot, to prevent abuse and bot-loops (Discord.js's quickstart guide prevent's bots from executing commands as shown here ). So, your best bet is probably to implement the !coin command yourself.

You can make the bot send a fake !coin message and then execute the script contained in the coin command using another built-in command. Here is how you could adjust your current code for mytask :

@client.command()
async def coin(ctx):
    ...  # Add coin code here

@tasks.loop(seconds=60)
async def mytask(ctx):
    # Send a fake "!coin" message
    channel = bot.get_channel(305347032569348107)
    await channel.send('!coin')

    # Execute the coin command
    await channel.invoke(client.get_command('coin'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM