【问题标题】:Discord.py bot - how to make bot copy my messageDiscord.py bot - 如何让 bot 复制我的消息
【发布时间】:2021-08-23 06:53:00
【问题描述】:

我正在使用 Python 进行开发。我想让我的机器人复制上一条消息,其中包含一个特殊的命令。

例如,我输入命令say。所以它看起来像这样:

我:说你在做什么?

机器人:你在做什么?

【问题讨论】:

  • 到目前为止你尝试过什么?如果您提供minimal reproducible example 总是有帮助的。
  • 说实话,我不知道,所以这就是我问的原因:(。我尝试了@Seekii的代码:@bot.listen() async def on_message(message): if message.content.startswith("!say"): await message.channel.send(message.content)它显示了正确的结果,但一个减号是机器人也说!say 命令,但不应该。

标签: python discord bots


【解决方案1】:

这对我有用:

@bot.command()
async def say(ctx,*,args): 
    await ctx.send(args)

【讨论】:

  • 问题是我在每个命令中都输入了我的机器人if,而不是async,所以它只是显示一个错误。难道你不知道,如何将其重新格式化为if 类型?
【解决方案2】:
@bot.listen()
async def on_message(message):
    if message.content.startswith("!say"):
        await message.channel.send(message.content)

【讨论】:

  • 这确实很有帮助,但现在它会复制整个命令。我的意思是我的机器人也在说命令。有什么办法可以从bot的消息中删除!say
  • @Aaron 试试echo = message.content.split(" ", 1)[1]await message.channel.send(echo)
  • 它有帮助!请接受我真诚的感谢,@UziGoozie
【解决方案3】:

感谢@UziGoozie 评论和@Seekii 代码,这个问题的解决方法是下一个代码:

async def on_message(message):
    echo = message.content.split(" ", 1)[1]
    if message.content.startswith("!say"):
    await message.channel.send(echo)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 2020-09-11
    • 1970-01-01
    • 2018-07-24
    • 2021-04-04
    • 2020-10-04
    • 2021-12-03
    • 2021-10-04
    相关资源
    最近更新 更多