调用该命令时C.send
,server.db 数据库应该添加到选定的 id + 1 计数,但这不会发生。可能是什么问题?
import discord
import random
import time
from datetime import datetime,timedelta
from discord.ext import commands
from discord.ext.commands import MissingPermissions
from discord.utils import get
from discord.ext.commands import cooldown, BucketType
import asyncio
import sqlite3
import functools
import operator
bot = commands.Bot(command_prefix = "C.",intents = discord.Intents.all())
@bot.event
async def on_ready():
print("Hi!\nBot {NAME} is ready!")
print('------------------')
db = sqlite3.connect('server.db')
sql = db.cursor()
sql.execute("""CREATE TABLE IF NOT EXISTS users (
id INT,
name TEXT,
count INT,
server_id INT
)""")
db.commit()
for guild in bot.guilds:
guild_id = guild.id
guild = bot.get_guild(guild_id)
for member in guild.members:
role = discord.utils.get(guild.roles, name="role_create")
if role not in guild.roles:
await guild.create_role(name="role_create")
print(f'Роль для {guild.name} создана!')
if role in member.roles:
if sql.execute(f'SELECT id FROM users WHERE id = {member.id}').fetchone() is None:
sql.execute(f"INSERT INTO users VALUES ({member.id}, '{member}', 0, {member.guild.id})")
db.commit()
else:
user_id_to_random = []
for value in sql.execute("SELECT * FROM users"):
print(value)
for user_id_in_sql in sql.execute(f"SELECT id FROM users"):
convert = functools.reduce(operator.add, (user_id_in_sql))
user_id_to_random.append(convert)
else:
global r
r = random.choice(user_id_to_random)
@bot.command()
async def send(ctx):
sql.execute(f"UPDATE users SET count = count + 1 WHERE id = {r} ")
db.commit()
bot.run(TOKEN)
最有可能的是,事实是您在本地创建 db、sql 变量,尝试将它们从函数中取出,一切都应该正常