【问题标题】:Delete channel on reaction // Ticket Bot discord.py根据反应删除频道 // Ticket Bot discord.py
【发布时间】:2021-07-13 13:23:10
【问题描述】:

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

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 首先我需要再次定义频道,但是作为频道名称,所以 client.get_channel(payload.channel_id)。当我尝试它似乎没有工作时
    • print(channel.name) 看看它是否存在,使用get_channel 将使用缓存,它可能不存在因此您需要使用fetch_channel。我编辑了上面的代码
    猜你喜欢
    • 2020-10-11
    • 2021-01-25
    • 2021-11-08
    • 1970-01-01
    • 2019-07-07
    • 2021-10-04
    • 2021-12-21
    • 2021-02-27
    • 2018-09-10
    相关资源
    最近更新 更多