如何检查频道是否已存在?

时间:2021-02-20 14:37:58

标签: python sqlite discord discord.py

我有一个票务系统,有了这个系统,也可以根据反应创建票证。但是,您可以根据需要多次按下反应,并且新频道不断涌现。我怎样才能防止这种情况?

我的代码:

    @commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        user = self.bot.get_user(payload.user_id)
        guild = self.bot.get_guild(payload.guild_id)
        if payload.user_id == self.bot.user.id or user.bot:
            return
        cursor = reaction_conn.cursor()
        cursor.execute('CREATE TABLE IF NOT EXISTS ticket_messages ("message_id" INT, "reaction" TEXT)')
        cursor.execute('SELECT reaction FROM ticket_messages WHERE message_id = ?', (payload.message_id,))
        row = cursor.fetchone()
        cursor.close()
        reaction_conn.commit()
        if row is not None and row[0] == str(payload.emoji):
            await self.createTicket(guild, user, None, f "Created via reaction.")
            channel = self.bot.get_channel(payload.channel_id)
            message = await channel.fetch_message(payload.message_id)
            await message.remove_reaction(payload.emoji, guild.get_member(payload.user_id))

我尝试通过 if channel 参数进行处理,但这不起作用。

评论后编辑:

    def enable_reactions(self, message_id: int, reaction: str, reactor_id: int):
        cursor = reaction_conn.cursor()
        cursor.execute('CREATE TABLE IF NOT EXISTS ticket_messages ("message_id" INT, "reaction" TEXT, "reactor_id" INT)')
        cursor.execute('INSERT INTO ticket_messages(message_id, reaction, reactor_id) VALUES (?, ?, ? )',
                       (message_id, reaction, reactor_id))
        cursor.close()
        reaction_conn.commit()
        log.debug(f'Ticket per reaction created for message ID {message_id}')

还有新的 listener

    @commands.Cog.listener()
        #Other stuff
        cursor = reaction_conn.cursor()
        cursor.execute('CREATE TABLE IF NOT EXISTS ticket_messages ("message_id" INT, "reaction" TEXT, "reactor_id" INT)')
        cursor.execute('SELECT reaction FROM ticket_messages WHERE reactor_id = ?', (payload.user_id,))
        row = cursor.fetchone()
        cursor.close()
        reaction_conn.commit()
        if row is not None and row[0] == str(payload.emoji):
            await self.createTicket(guild, user, None, f"Created via reaction.")# update this to add user_data
            channel = self.bot.get_channel(payload.channel_id)
            message = await channel.fetch_message(payload.message_id)
            await message.remove_reaction(payload.emoji, guild.get_member(payload.user_id))
        else:
            channel3 = self.bot.get_channel(812711563261247518)
            await channel3.send("You already have a ticket.") # ctx.send will not work

我仍然收到 qlite3.OperationalError: no such column: reactor_id 错误,但我认为可能与以下原因有关:

    @server_ticket.command(name='reaction', aliases=["setup", "reactions"])
    @commands.has_permissions(manage_messages=True)
    async def server_ticket_reaction(self, ctx):
        """Allows a server mod to setup a message to enable ticket creation per reaction"""
        timeout_embed = discord.Embed(
            description="Oops! You took to long, the setup process has been canceled. Try again!", color=self.bot.color)

        def channel_check(msg):
            return discord.utils.get(ctx.guild.text_channels,
                                     mention=msg.content) is not None and msg.author.id == ctx.author.id and msg.channel.id == ctx.channel.id

        def emoji_check(reaction, user):
            return reaction.message.id == emoji_msg.id and user == ctx.message.author and reaction.message.channel.id == ctx.channel.id

        def message_check(msg):
            return len(msg.content) < 2048 and msg.author.id == ctx.author.id and msg.channel.id == ctx.channel.id

        def title_check(msg):
            return len(msg.content) < 256 and msg.author.id == ctx.author.id and msg.channel.id == ctx.channel.id

        channel_embed = discord.Embed(
            description="Mention the **channel** where you want to have the ticket creation message.",
            color=self.bot.color)
        channel_embed.timestamp = datetime.datetime.utcnow()
        await ctx.send(embed=channel_embed)
        try:
            channel_mention = await self.bot.wait_for('message', timeout=45.0, check=channel_check)
            channel = discord.utils.get(ctx.guild.text_channels, mention=channel_mention.content)
        except asyncio.TimeoutError:
            return await ctx.send(embed=timeout_embed)
        emoji_embed = discord.Embed(
            description="Now **react** with the emoji you want to use for ticket creation to **this** "
                        "message.\n:warning: Please use an emoji from this server or an Unicode emoji.",
            color=self.bot.color)
        emoji_embed.timestamp = datetime.datetime.utcnow()
        emoji_msg = await ctx.send(embed=emoji_embed)
        try:
            reaction = await self.bot.wait_for('reaction_add', timeout=45.0, check=emoji_check)
        except asyncio.TimeoutError:
            return await ctx.send(embed=timeout_embed)
        message_embed = discord.Embed(title="Title", description="Content", color=self.bot.color)
        message_embed.set_footer(text=self.bot.user.name, icon_url=self.bot.user.avatar_url)
        await ctx.send(
            "What should the message contain?\n\nThe format is:\n**Message\nTitle\nContent**\n\n*This will be the "
            "**Message** before the embed.*",
            embed=message_embed)
        try:
            message = await self.bot.wait_for('message', timeout=120.0, check=message_check)
        except asyncio.TimeoutError:
            return await ctx.send(embed=timeout_embed)
        try:
            title = await self.bot.wait_for('message', timeout=120.0, check=title_check)
        except asyncio.TimeoutError:
            return await ctx.send(embed=timeout_embed)
        try:
            content = await self.bot.wait_for('message', timeout=120.0, check=message_check)
        except asyncio.TimeoutError:
            return await ctx.send(embed=timeout_embed)
        try:
            reaction_embed = discord.Embed(title=title.content, description=content.content, color=self.bot.color)
            # reaction_embed.set_footer(text=self.bot.user.name, icon_url=self.bot.user.avatar_url)
            reaction_embed.timestamp = datetime.datetime.utcnow()
            reaction_embed.set_footer(text="To create a ticket, react to the icon", icon_url=self.bot.user.avatar_url)
            ticket_msg = await channel.send(message.content, embed=reaction_embed)
            await ticket_msg.add_reaction(reaction[0].emoji)
            self.enable_reactions(ticket_msg.id, str(reaction[0].emoji))

它在最后一个 ) 表示参数 reactor_id 未填充。

2 个答案:

答案 0 :(得分:0)

您可以修改表格以包含用户数据

@commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        # other stuff
        cursor = reaction_conn.cursor()
        cursor.execute('CREATE TABLE IF NOT EXISTS ticket_messages ("message_id" INT, "reaction" TEXT, "reactor_id" INT)')
        cursor.execute('SELECT reaction FROM ticket_messages WHERE reactor_id = ?', (payload.user_id,))
        row = cursor.fetchone()
        # other stuff
        if row is not None and row[0] == str(payload.emoji):
            await self.createTicket(guild, user, None, f "Created via reaction.")# update this to add user_data
        else:
            await ctx.send(f"You have an open ticket")

我们正在使用 user_id 来限制每个用户一个活动的股票代码,如果您希望用户在当前工单关闭后创建更多工单,则必须删除该行或将其设置为默认值 (0) 如果您想再用一次。

答案 1 :(得分:0)

我在文档中看不到任何 reactor_id。 有 user_id 也许你可以尝试替换它。 https://discordpy.readthedocs.io/en/latest/api.html?highlight=on_raw_reaction#discord.RawReactionActionEvent