繁体   English   中英

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

[英]How to make python discord bot execute/use a command

这就是我现在所拥有的,它确实每 60 秒成功发送一次消息。

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

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

我也试过这个await channel.send(!coin)但这甚至没有运行并显示SyntaxError: invalid syntax error。

简单地说,请记住,您的机器人只负责将!coin发送到任何渠道。 由实际处理!coin命令的机器人决定是否要运行与!coin命令相关的任何逻辑并响应或不响应。
大多数 discord 机器人甚至不会考虑来自另一个机器人的“命令”,以防止滥用和机器人循环(Discord.js 的快速入门指南阻止机器人执行命令,如此处所示)。 因此,您最好的选择可能是自己实现!coin命令。

您可以让机器人发送虚假的!coin消息,然后使用另一个内置命令执行coin命令中包含的脚本。 以下是如何为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