简体   繁体   English

如何使 python discord 机器人执行/使用命令

[英]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.这就是我现在所拥有的,它确实每 60 秒成功发送一次消息。

@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.但是,当我将await channel.send('Example message')更改为await channel.send('!coin')时,它只会发送 .coin 消息,而不是执行/使用 !coin 命令。

and I also tried this await channel.send(!coin) but this doesn't even run and shows SyntaxError: invalid syntax error.我也试过这个await channel.send(!coin)但这甚至没有运行并显示SyntaxError: invalid syntax error。

Simply, keep in mind that your bot is only responsible for sending !coin to whichever channel.简单地说,请记住,您的机器人只负责将!coin发送到任何渠道。 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.由实际处理!coin命令的机器人决定是否要运行与!coin命令相关的任何逻辑并响应或不响应。
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 ).大多数 discord 机器人甚至不会考虑来自另一个机器人的“命令”,以防止滥用和机器人循环(Discord.js 的快速入门指南阻止机器人执行命令,如此处所示)。 So, your best bet is probably to implement the !coin command yourself.因此,您最好的选择可能是自己实现!coin命令。

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.您可以让机器人发送虚假的!coin消息,然后使用另一个内置命令执行coin命令中包含的脚本。 Here is how you could adjust your current code for mytask :以下是如何为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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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