【问题标题】:discord.py module how do i remove the command text afterwards?discord.py 模块之后我如何删除命令文本?
【发布时间】:2017-05-14 11:30:47
【问题描述】:
from discord.ext.commands import Bot

import secrets
from time import sleep

discordBot = Bot(command_prefix = ".")

listStrings = ['add a reaction', 'adnd']

@discordBot.event
async def on_read():
    print('Client logged in')


@discordBot.command()
async def s(*args):
        return await discordBot.say (" ".join(args))

@discordBot.command()
async def close():
    quit()


discordBot.run(secrets.token)

我想说“.s(文本)”并且机器人说文本(已经有效)但删除了您的消息,我该怎么做?我得到了 on_message 的部分工作:

@discordbot.event
async def on_message(message):
    await discordBot.delete_message(message)

最好的方法是什么?提前致谢。

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    很抱歉,您已经 26 天没有得到答复。如果你还在寻找答案,希望我能提供一些帮助。所以我运行了你的代码,机器人运行了命令,我的消息被删除了,然后机器人的消息也被删除了。解决此问题的一种方法是提供 if 语句,这意味着机器人仅在作者不是机器人时才会删除消息。

    @test_bot.event
    async def on_message(message):
        if not (message.author).bot:
            await test_bot.delete_message(message)
        await test_bot.process_commands(message)
    

    另一种只删除作为命令的消息的方法是使用另一个 if 语句。以下代码是特定的,因此您只删除命令,而不是所有用户的消息(如果它们不是机器人)。以下代码使用message.content 将消息作为字符串返回。

    @test_bot.event
    async def on_message(message):
        if message.content.startswith('.s'):
            await test_bot.delete_message(message)
        await test_bot.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-27
      • 2021-09-26
      • 2021-02-08
      • 1970-01-01
      • 2021-07-27
      • 2019-03-24
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多