【问题标题】:I am making a modmail bot with discord.py and The bot is creating text channels with every message我正在使用 discord.py 制作一个 modmail 机器人,该机器人正在为每条消息创建文本通道
【发布时间】:2021-09-12 07:33:51
【问题描述】:
@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if isinstance(message.channel, DMChannel):
        guild = bot.get_guild(guildID) 
        channels = await guild.fetch_channels()
        channel = discord.utils.get(channels, name=message.author.id)
        message1 = message.author
        embedVar = discord.Embed(title= f'{message.author} has sent a new message', description = message.content, 
        color= 0xff0000)
        category = discord.utils.get(guild.categories, id = category.id) 
        channel = await guild.create_text_channel(message.author.id, category=category, id = message.author.id)
        embedMod = discord.Embed(title = 'Information for the mods', description = '!close- Closes the text channel'
        '\n!DM- To DM the user anonymously', color = 0x00FF00)
        
        if channel not in channels:
        await guild.create_text_channel(message.author.id, category = category)
        await channel.send(embed=embedMod)
        if channel in channels:
        pass    
        await channel.send(embed=embedVar)
        
    
    await bot.process_commands(message)

我正在制作一个机器人,用户通过 dms 向机器人寻求帮助,机器人在公会中创建一个特定类别下的文本频道。问题是每次向机器人发送消息时,机器人都会为每条消息创建一个文本通道,即使它是同一用户。如果机器人已经创建并且同一用户的其他消息已发送到该频道,我如何确保该机器人不会创建具有相同名称的文本频道..

【问题讨论】:

  • 您需要将 user_id 和“his”channel_id 保存到 db 或 json 文件中。然后你可以检查用户是否已经有票
  • 我该怎么做?你能帮助我吗? @古迪
  • 循环遍历catagory.channels并检查if channel.topic == str(message.author.id)
  • 它没有用,它仍在创建新频道。还有其他想法吗?
  • 您可以使用当前代码编辑您的帖子吗? :D

标签: python discord.py


【解决方案1】:

我是否正确理解了您的代码,频道名称是 dm'ed 机器人的用户的用户 ID?如果是这样,您只需检查是否已存在具有该名称的频道并将您的消息重定向到那里。如果没有,请创建频道

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if isinstance(message.channel, DMChannel):
        guild = bot.get_guild(guildID) 
        channels = await guild.fetch_channels()
        channel = discord.utils.get(channels, name=message.author.id)
        message1 = message.author
        embedVar = discord.Embed(title= f'{message.author} has sent a new message', description = message.content, 
        color= 0xff0000)
        category = discord.utils.get(guild.categories, id = category.id)
        embedMod = discord.Embed(title = 'Information for the mods', description = '!close- Closes the text channel'
        '\n!DM- To DM the user anonymously', color = 0x00FF00)
        
        if channel == None: #Channel doesn't exist yet. So create it
            channel = await guild.create_text_channel(message.author.id, category=category, id = message.author.id)
        
        await channel.send(embed=embedMod)
        await channel.send(embed=embedVar)
    
    await bot.process_commands(message)

【讨论】:

  • 我尝试了您的代码,但机器人仍在为我发送给机器人的每条消息创建文本通道。
猜你喜欢
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 2019-09-12
  • 1970-01-01
相关资源
最近更新 更多