【问题标题】:ModMail Bot keeps opening channels for every single message Discord.PyModMail Bot 不断为每条消息打开通道 Discord.Py
【发布时间】:2020-12-13 02:41:50
【问题描述】:

我正在制作一个不和谐的 ModMail 机器人,并且我已经做到了,当有人 dms 机器人时会打开一个频道,但发生的情况是,每次有人 DM 机器人时,它都会打开另一个频道。例如,如果我有一次向机器人发送 10 条消息的对话; 10 个不同的渠道将被打开。有人知道我怎样才能把它发送到现有的吗?我尝试过使用检查,但我不确定如何实施它们。这是我的代码:

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

    if message.author != message.author.bot:
      await message.add_reaction('✅')
      if not message.guild:
        guild = client.get_guild(guild)
        c = None
          
        for channel in guild.text_channels:  # check every channel
           if channel.name == "❗ticket-{message.author.id}":
            c = channel  # channel found, stop looking
            break

    if c is None:  # if no channel was found
      c = await guild.create_text_channel('❗ticket-{message.author.id}')
      
      embed = Embed(color=discord.Color.orange())
      embed.add_field(name="**????ModMail Support????**",
                            value=f"User Mention: {message.author.mention}\nUsername: **{message.author}**\nUser-ID: **{message.author.id}**\n\n__**Content**__\n{message.content}")
           
          
      await c.send(embed=embed)

            

提前非常感谢! :)

【问题讨论】:

  • 那个代码没有任何意义

标签: discord.py discord.py-rewrite


【解决方案1】:

您可以尝试检查该公会中是否已存在具有该名称的频道。您可以通过Guild.text_channels获取公会频道列表。

c = None

for channel in guild.text_channels:  # check every channel
    if channel.name == "❗ticket-{message.author.id}":
        c = channel  # channel found, stop looking
        break

if c is None:  # if no channel was found
    c = ..  # create the channel

请记住,并非所有字符都可以在频道名称中(如空格),不能有大写字母,并且您可以有重复,因此您可能希望使用用户的id 创建一个频道,并且将他们的display name@mention 添加到频道描述以了解他们的名字。

【讨论】:

  • 它仍然无法正常工作,但我认为那是因为我对此没有太多经验。我现在将用我的代码编辑问题。我想我把它放在了错误的地方。
  • 是的,您的缩进已关闭,message.author != message.author.bot 并没有做很多事情。你想要if not message.author.bot(它返回一个boolean)。查看我的答案中的缩进并将其与您的进行比较。
猜你喜欢
  • 2020-10-26
  • 2021-09-12
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2021-12-04
  • 1970-01-01
  • 2020-09-11
相关资源
最近更新 更多