[Python] discord.py | How to import channel id from database to send logs from bot

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I made a database on SQLite3, there is guild ID and channel ID which one choosed from user to send logs here. I want the user to be able to independently select a channel to send all logs to and after that bot will sends the logs to the channel specified by the user

    I messed around with the code a lot because it turned out to be complete garbage but I made a command to select a channel and now it's writing the channel ID into the database:

    @app_commands.command(name="логи", description="устанавливает канал для отправки логов")
    @app_commands.check(is_owner)
    async def logs(self, interaction: discord.Interaction):
    connection = sqlite3.connect("./cogs/main.db")
    cursor = connection.cursor()
    cursor.execute("UPDATE Guilds SET channel_id = ? WHERE guild_id = ?", (interaction.channel.id, interaction.guild.id))
    connection.commit()
    connection.close()
    logs = discord.Embed(title="Готово!", description=f"Теперь логи привязаны к каналу - {interaction.channel.mention}", color=discord.Color.purple())
    logs.set_footer(text=f"Команду отправил {interaction.user}", icon_url=interaction.user.avatar)
    await interaction.response.defer()
    await interaction.followup.send(embed=logs)


    From here the bot gets information about which channel the user asks to send logs to, but now I need the bot to send these same logs to the specified channel

    To begin with, I wanted to try to make the bot send delete messages to a channel specified by the user but I messed it up a lot:

    @commands.Cog.listener()
    async def on_message_delete(self, message, interaction: discord.Interaction, channel: discord.TextChannel):
    if message.author.bot:
    return
    connection = sqlite3.connect("./cogs/main.db")
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM Guilds WHERE guild_id = ?", (channel.guild.id,))
    result = cursor.fetchone()
    if result:
    channel_id = result[1]
    if channel_id:
    await discord.TextChannel == channel_id
    message_deleted_msg = discord.Embed(title=f"Было удалено сообщение в канале {message.channel.mention}", description=f"**Участник:** {message.author.mention} | {message.author}/{message.author.id}\n\n**Содержимое:**\n{message.content}", color=discord.Color.dark_purple())
    message_deleted_msg.add_field(name=f"**Дата/время:**", value=f"{message.created_at.replace(tzinfo=timezone.utc).strftime('%Y-%m-%d / %H:%M:%S')}")
    message_deleted_msg.set_footer(text=f"ВНИМАНИЕ: Время удаления указывается в UTC+0")
    await channel_id.send(embed=message_deleted_msg)


    How can I fix this garbage code?

    Continue reading...

Compartilhe esta Página