繁体   English   中英

使用 Discord.py 中的命令在设定时间内禁用 Discord 机器人

[英]Disabling a discord bot for a set time using a command in Discord.py

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

我正在尝试制作一个机器人,当服务器中的某人在任何上下文中使用“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”本身而不是一个词时才做出回应?我试着让它前后有空格,但如果 a消息以它开始或结束。我想这可能不值得发布自己的帖子,因为我可能只是愚蠢的lmao)

您可以使用全局变量来存储值并从不同位置对其进行编辑

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM