discord.py bot 每次发送消息并仅在发送每个单词时完成

时间:2021-03-22 12:08:01

标签: python discord.py

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

@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)

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

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

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

3 个答案:

答案 0 :(得分: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)

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

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

答案 1 :(得分:0)

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...]

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

答案 2 :(得分:0)

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

像这样:

@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)