【问题标题】:Disabling a discord bot for a set time using a command in Discord.py使用 Discord.py 中的命令在设定的时间内禁用不和谐机器人
【发布时间】:2021-07-06 15:14:50
【问题描述】:

我一直在尝试解决这个问题,但鉴于我大部分时间只是将其他人的代码放在一起并尝试解决问题所在,因此我缺乏执行此操作所需的大量知识。

我正在尝试制作一个机器人,当服务器中的某人在任何上下文中使用“the”一词时(即使没有机器人的前缀),它也会用“da”来纠正它们(这是一个笑话,拜托不要为它烤我lmfao)。有了它的作用,它可能很烦人 - 所以我希望人们能够一次禁用它大约 5 分钟,并且让它不发送消息。问题是,我对 python 的经验太少了,我基本上只是在这里猜测。我将如何制定一个命令,该命令在触发时会阻止机器人在一定时间内发送消息?

现在,它发送消息的方式是检查消息是否包含“the”,那么是否有可能让它在发送之前也检查布尔值是否为真?

这是负责发送的主要代码块。我知道这可能不是最理想的,如果您愿意,请随时向我解释如何让它变得更好!

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if ('the') in message.content:
        await message.channel.send('i think you meant da :rolling_eyes:')

谢谢!

(另外,有没有什么方法可以让它只响应“the”本身而不是一个词?我试着让它前后都有空格,但那没有如果消息以它开始或结束,则工作。我认为这可能不保证它自己的帖子,因为我可能只是愚蠢的 lmao)

【问题讨论】:

  • 你使用commands.Bot还是discord.Client

标签: python discord.py


【解决方案1】:

你可以使用一个全局变量来存储一个值并从不同的地方编辑它

correct_the = True

@client.event
async def on_message(message):
    global correct_the
    if message.author == client.user:
        return

    words = message.content.lower().split(" ") # make a list of all words
    # like this -> ["the", "dogs", "and", "cats"]
    if 'the' in words:
        if correct_the: # check if it should correct it
            await message.channel.send('i think you meant da :rolling_eyes:')

    if message.content.startswith("!disable"): # or make this as command if you use commands.Bot
        if not correct_the:
            print("already disabled")
        else:
            correct_the = False
            await asyncio.sleep(5*60) # sleeps 5min
            correct_the = True

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2021-12-05
    • 2023-03-20
    • 2018-11-10
    • 2021-10-30
    • 2021-12-31
    • 2018-11-17
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多