【问题标题】:discord.py bot sends message each and finishes only when sending each wordsdiscord.py bot 每次发送消息,并且仅在发送每个单词时完成
【发布时间】:2021-03-22 12:08:01
【问题描述】:

我是这方面的新手,机器人会一个接一个地发送每个坏词直到它完成,这真的很烦人。这是代码。

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

  with open('badwords.txt') as file:
    file = file.read().strip()

  for badwords in file:
    if badwords in message.content.lower():
       msg = await message.channel.send(f'{message.author.mention}Please avoid using malicious and insulting words in the future! words you used: {badwords}',delete_after=10)
       await msg.add_reaction('<a:cRight:819401530964836382>')
       await msg.add_reaction('<a:Heart:819401449095692309>')
       await msg.add_reaction('<a:cLeft:819403565440827423>')
       await message.channel.send('messages delete after 10 seconds',delete_after=5)
       guild = message.guild
       role = discord.utils.get(guild.roles, name="Muted")
       await message.author.add_roles(role)
       embed = discord.Embed(title="Muted!", description=f"{message.author.mention} Muted for 1 minute", colour=discord.Colour.blue())
       embed.add_field(name="reason",value=f"Badwords moderation, word that was used: {badwords}",inline=False)
       await message.channel.send(embed=embed,delete_after=300)
       await asyncio.sleep(60)
       await message.author.remove_roles(role)
       embed = discord.Embed(title="Unmuted", description=f"Unmuted - {message.author.mention} ", colour=discord.Colour.blue())
       await message.channel.send(embed=embed,delete_after=300)

  await client.process_commands(message)

我正试图让机器人在一条消息中发送所有坏话,就像在代码中一样, 机器人会发送一条消息并静音,但它会根据消息中有多少坏词重复,并且还会重复静音在她/他的消息中使用坏词的用户。 代码没有问题,它工作得非常好,我要求你进行更改,无论消息中有多少坏词,都只让机器人发送一次,并且还要列出机器人发送的消息中的坏词。

在图像中它会重复直到发送完所有单词,我完全不知道应该添加或更改什么以使机器人编译每个坏词并发送嵌入并静音使用过的用户坏话

(忽略这一点,似乎堆栈溢出一直在告诉我添加细节......我正在填充它) 我在这段代码中使用了打开文件的方法,请帮助和 ps 我是新手不要说我不好,我还在 14 岁学习编码:) 我会很高兴有人会帮助我:)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您的机器人正在发送多条消息,因为您的逻辑存在于 forloop 中,这意味着对于“坏词”的每个实例,您的机器人将执行给定的指令集。

    缓解这种情况的一种方法是构建一个单词列表,您希望警告用户不要使用 forloop,然后发送一条包含所有上下文的消息。例如:

      included_badwords = []
      for badwords in file:
        if badwords in message.content.lower():
           included_badwords.append(badwords)
      
      msg_content = '{0} Please avoid using malicious and insulting words in the future! words you used: {1}'.format(message.author.mention, ','.join(included_badwords))
      msg = await message.channel.send(msg_content, delete_after=10)
      await msg.add_reaction('<a:cRight:819401530964836382>')
      await msg.add_reaction('<a:Heart:819401449095692309>')
      await msg.add_reaction('<a:cLeft:819403565440827423>')
      await message.channel.send('messages delete after 10 seconds',delete_after=5)
      guild = message.guild
      role = discord.utils.get(guild.roles, name="Muted")
      await message.author.add_roles(role)
      embed = discord.Embed(title="Muted!", description=f"{message.author.mention} Muted for 1 minute", colour=discord.Colour.blue())
      embed.add_field(name="reason",value=f"Badwords moderation, word that was used: {badwords}",inline=False)
      await message.channel.send(embed=embed,delete_after=300)
      await asyncio.sleep(60)
      await message.author.remove_roles(role)
      embed = discord.Embed(title="Unmuted", description=f"Unmuted - {message.author.mention} ", colour=discord.Colour.blue())
      await message.channel.send(embed=embed,delete_after=300)
    

    我还建议将您的单词表加载到内存中,而不是每次收到消息时都从文件中读取。

    希望这会有所帮助,如果您需要进一步的帮助,请随时联系我。

    【讨论】:

    • 啊,是的,.format() 是一个简单而优雅的改进
    【解决方案2】:

    如果您循环浏览您的坏词文件并将所有使用过的词存储在一个列表中,您可以随后发送一条包含所有收集到的坏词的消息。

    类似这样的:

    @client.event
    async def on_message(message):
      if message.author == client.user:
          return
      if message.author.bot: 
          return
    
      with open('badwords.txt') as file:
        file = file.read().strip()
    
      found_words = list()
    
      for badword in file:
        if badword in message.content.lower():
          found_words.append(badword)
    
      msg_str = f'{message.author.mention}Please avoid using malicious and insulting words in the future! words you used: {", ".join(found_words)}'
      msg = await message.channel.send(msg_str,delete_after=10)
      await msg.add_reaction('<a:cRight:819401530964836382>')
      await msg.add_reaction('<a:Heart:819401449095692309>')
      await msg.add_reaction('<a:cLeft:819403565440827423>')
      await message.channel.send('messages delete after 10 seconds',delete_after=5)
      guild = message.guild
      role = discord.utils.get(guild.roles, name="Muted")
      await message.author.add_roles(role)
      embed = discord.Embed(title="Muted!", description=f"{message.author.mention} Muted for 1 minute", colour=discord.Colour.blue())
      embed.add_field(name="reason",value=f"Badwords moderation, word that was used: {", ".join(found_words)}",inline=False)
      await message.channel.send(embed=embed,delete_after=300)
      await asyncio.sleep(60)
      await message.author.remove_roles(role)
      embed = discord.Embed(title="Unmuted", description=f"Unmuted - {message.author.mention} ", colour=discord.Colour.blue())
      await message.channel.send(embed=embed,delete_after=300)
    
      await client.process_commands(message)
    
    

    【讨论】:

      【解决方案3】:

      for 循环每次检查一个单词是否在消息中,如果是这样,它会执行整个过程。不是最优的!

      解决您的特定问题的一个简单改进可能是

      # first define an empty list where you can put eventual bad words used
      badwords_list = []
      
      # add words to the list if they appear
      for badwords in file:
        if badwords in message.content.lower():
          badwords_list.append(badwords)
      
      # assuming the list is not empty, join its content in a single string and send the message as you did before
      if badwords_list:
        all_badwords = ','.join(badwords_list)
        msg = await message.channel.send(f'{message.author.mention}Please ... words you used: {all_badwords}',delete_after=10)
      
        [...etc etc etc...]
      

      但是像这样遍历文件中的所有单词的事实......我认为可能有更好的解决方案

      【讨论】:

        猜你喜欢
        • 2021-11-29
        • 2021-07-26
        • 2020-10-15
        • 2019-04-14
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多