如何阻止discord.py bot在SQLite数据库中增加超过预期的值?

btqmn9zl  于 2022-12-29  发布在  SQLite
关注(0)|答案(1)|浏览(117)

I've recently implemented an SQLite database into my discord.py bot in an effort to teach myself SQL in a fun and more meaningful way. It works as intended with a slight issue. The function of the cog is to increment a counter for each discord user everytime they send 'brilliant' in the chat. However, it is incrementing the value for each user several times when they send 'brilliant' and sometimes users that have not said 'brilliant' pop up in the database. The original code:

import discord, sqlite3
from discord.ext import commands

class brilliant_count(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author == self.bot.user:
            return
        if message.content.lower() == "brilliant" or "brilliant!":

            conn = sqlite3.connect(r"./cogs/brilliant_count.db")
            c = conn.cursor()

            # Get the user data
            user = message.author

            # Check if the user is already in the database
            c.execute("SELECT count FROM brilliant_counter WHERE discord_id=?", (user.id,))
            result = c.fetchone()
            if result is None:
                # User is not in the database, add them with a count of 1
                c.execute("INSERT INTO brilliant_counter (discord_id, discord_username, count) VALUES (?, ?, ?)", (user.id, user.name, 1))
            else:
                # User is in the database, increment the count
                count = result[0]
                count += 1
                c.execute("UPDATE brilliant_counter SET discord_username=?, count=? WHERE discord_id=?", (user.name, count, user.id))

            # Commit the changes to the database
            conn.commit()

            # Close the database connection
            conn.close()


async def setup(bot):
    await bot.add_cog(brilliant_count(bot))

我想这可能是因为轮齿中的"on_message"事件侦听器为一条消息被多次触发,也可能是因为机器人在多个不和谐的组中。
雅各布森只说过几次"辉煌",林从来没有送过"辉煌",但他们的数字非常高。

做了一些研究,我得出了这样的结论:

if message.content.lower() == "brilliant" and not message.processed:
            message.processed = True

But the above code doesn't work with discord.py and I can't seem to find any other documentation for it. I'd appreciate some advice on what to do next. many thanks.
编辑:已修复。已替换

if message.content.lower() == "brilliant" or "brilliant!":

与:

if (message.content.lower() == "brilliant") or (message.content.lower() == "brilliant!"):

与第一行一样,机器人将其视为真,并递增发送到数据库的每一条消息。

ghg1uchk

ghg1uchk1#

不是答案,但这是渴望一个评论:

message.content.lower() == "brilliant" or "brilliant!"

并不像你期望的那样工作。

if 1==2 or "brilliant!":
    print('hi')

将打印,因为即使1==2为假,“brilliant!”本身也将为真(每个非空字符串都为真)。请注意下一行中的(),它什么也不做,但在我看来,它在阅读代码时帮助很大。
我不确定如果消息仅包含单词“brilliant”是否要触发计数器,但如果条件应为:

if (message.content.lower() == "brilliant") or (message.content.lower() == "brilliant!"):

或许

if "brilliant" in message.content.lower():

如果你也算上“超级聪明”的话。

相关问题