【问题标题】:on_message function repeating, Discord.py rewriteon_message 函数重复,Discord.py 重写
【发布时间】:2019-08-23 12:17:32
【问题描述】:

我正在制作一个机器人,当它检测到您正在使用禁用词时,它会删除您的消息。很简单,但是,当我这样做时。 on_message 函数正在重复。我不知道为什么,但我希望你能回答我的问题

@client.event
async def on_message(msg):
    contents = msg.content.split(" ")
    for word in contents:
        if word.lower() in chat_filter: #ChatFilter is a list of words that cannot be used
            try:
                await msg.delete()
                await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
            except discord.errors.NotFound:
                return

【问题讨论】:

  • 你能详细说明repeats itself是什么意思吗?
  • @reportgunner 好吧,我正在关注一个 youtube 视频。对他来说,当他编写代码时,它为他编写了一次。我复制了他的代码,它不断重复。我认为这是版本和东西的区别
  • 我问,因为我不知道您的意思是“重复自身”,如“无限循环运行”或“消息重复”(即每条消息有两个响应)或“消息成倍增长”。
  • @reportgunner 是的。即使我不希望它发生,这些消息也会不断重复。有什么解决办法吗?我会给你视频的链接来告诉你我的意思youtube.com/watch?v=-2b1JUwEF3o
  • chat_filter 中的单词之一是否也在消息中?您可以添加一个检查以忽略您自己的消息:if msg.author == client.user: return

标签: python discord discord.py-rewrite


【解决方案1】:

您正在遍历消息中的每个单词,并为chat_filter 中的每个单词发送响应。相反,如果任何单词在禁止列表中,则发送一条消息:

@client.event
async def on_message(msg):
    contents = msg.content.split(" ")
    if any(word in chat_filter for word in contents):
        try:
            await msg.delete()
            await msg.channel.send("**YOU ARE NOT ALLOWED TO USE THIS WORD!!!**")
        except discord.errors.NotFound:
            return

【讨论】:

  • 这也是在做同样的事情。这是我要复制的视频 youtube.com/watch?v=-2b1JUwEF3o
  • 你好,又是我。我刚刚找到了解决方案。我不想告诉它,因为它是多么愚蠢。只是想感谢您帮助我找到解决方案
猜你喜欢
  • 1970-01-01
  • 2020-11-03
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 2020-05-31
相关资源
最近更新 更多