首页 > 解决方案 > 根据反应删除频道 // Ticket Bot discord.py

问题描述

我正在尝试制作一个票务机器人,这就是我目前所拥有的。现在我可以让机器人在检测到反应后创建一个频道,我需要能够让它关闭另一个反应的票,以及要添加的其他功能。但是这种反应是在创建的频道中,所以我不知道如何创建该频道 ID。有人建议我使用齿轮,但不知道从哪里开始。

async def on_raw_reaction_add(payload):
    channel = payload.channel_id
    if channel == 862711339298455563:
        guildid = payload.guild_id
        guild = client.get_guild(guildid)
        channel = guild.get_channel(payload.channel_id)
        message = channel.get_partial_message(payload.message_id)
        emoji = '️'
        member = payload.member
        

        payloaduserid = payload.user_id
        clientuserid = client.user.id   
        if payloaduserid != clientuserid: 
            await message.remove_reaction(emoji, member)
            category = client.get_channel(861003967000215583)
            channel1 = await guild.create_text_channel(f'ticket {payload.user_id}', category = category )

            ticketembed = discord.Embed(
                title = f'Ticket - {payload.user_id}',
                description = '- If you want to tag the admins please react with :telephone: \n - To report this message please react with   :warning: \n - To close the ticket react this mesaage with   :x:  ',
                color = discord.Color.red())
            ticketembed.set_footer(text = 'All of the conversation will be held in the archives, eventhought a moderator can delete a message in this channel, a copy of it will be held in a location where only the owner can access.')



            user = client.get_user(payload.user_id)
            await channel1.set_permissions(target=user, read_messages=True , send_messages=True)
            
            ticketembed1 = await channel1.send(embed= ticketembed)

            await ticketembed1.add_reaction('☎️')
            await ticketembed1.add_reaction('⚠️')
            await ticketembed1.add_reaction('❌') 
            await channel1.send(f'{user.mention}', delete_after = 0.1) ```

标签: pythondiscorddiscord.py

解决方案


一种方法是检查频道名称格式。有时从缓存中获取通道不是一种选择,因此我们可能需要获取它。

async def on_raw_reaction_add(payload):
    channel = client.get_channel(payload.channel_id)
    # if channel is not in cache then fetch is using a API request
    if not channel:
        channel = await client.fetch_channel(channel_id)
    # The user is the owner of the ticket since it is his id
    if channel.name == f"ticket {payload.user_id}":
        emoji = payload.emoji.name

        # delete 
        if emoji == "❌":
            await channel.delete()

最好保存频道 ID,然后使用它,因为频道名称可以更改。你可以像这样得到id。

channel1 = await guild.create_text_channel(f'ticket {payload.user_id}', category = category )
print(channel1.id)

推荐阅读