【问题标题】:How to make discord bot delete messages send by the bot如何使不和谐机器人删除机器人发送的消息
【发布时间】:2022-01-20 19:30:39
【问题描述】:

我制作了一个不和谐机器人,它通过命令将人员添加到队列中,然后显示队列。我不希望频道填满未完成的队列。所以我希望机器人删除自己的消息。

例子:

用户说:!dips

机器人说:1. 用户#这是消息 1。

另一个用户说:!dips

Bot 说:1. 用户,2. 另一个用户。 #这是消息 2。

机器人现在删除消息 1。由于消息 2 中的信息相同,因此不再需要它。

这是我正在使用的 python 代码。

@client.event 
async def on_message(message):

if message.content.lower() in list_of_commands:
    await message.channel.send(queue(arg1,arg2,arg3))
    #After sending new message here, it should delete the old message.

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    您可以将消息声明为变量,然后稍后将其删除。

    @client.event 
    async def on_message(message):
    
        if message.content.lower() in list_of_commands:
            msg = await message.channel.send(queue(arg1,arg2,arg3))
            # do your stuff
            await msg.delete()
    

    【讨论】:

    • 问题是我不知道如何将变量保存在 on_message() 函数之外。有没有办法返回变量。
    • 您可以在全局范围内使用变量或列表;然后在函数开头使用global variable_name,然后用消息值声明变量或将其附加到全局列表中,您可以在其他函数中使用它
    • 现在可以使用了,感谢您!以前从未见过 global,但这就是我所需要的。
    猜你喜欢
    • 2018-05-02
    • 2021-07-25
    • 1970-01-01
    • 2022-11-02
    • 2021-08-07
    • 2021-05-29
    • 1970-01-01
    • 2021-08-14
    • 2018-11-19
    相关资源
    最近更新 更多