Answer the question
In order to leave comments, you need to log in
When entering the server, the function responsible for entering data into the db does not work. The same thing happens with a function that is called by a command. what to do?
While working on a discord bot, I needed to use SQlite to store user data, for this two functions were created, one of which is activated when the user enters the server, and the other when the command is entered manually, another command was created to test the functions - testdatabase. But when entering the server, no information is entered into the database, and when using the command, it writes a message to the console stating that this command does not exist
import datetime
import sqlite3
import discord
from discord.ext import commands
from datetime import datetime
client = discord.ext.commands.Bot(command_prefix = "D.")
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_member_join(member):
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute('create table if not exists assignments(id TEXT, rawexp INTEGER, date TEXT, exp INTEGER, level INTEGER)')
userdate = datetime.now()
userid = member.author.id
userrawexp = 0
userexp = 0
userlevel = 1
cursorObj.execute("INSERT INTO assignments VALUES(?, ?, ?, ?, ?);", (userid, userrawexp, userdate, userexp, userlevel))
con.commit()
@client.command()
async def тестдатабазы(ctx):
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute("SELECT * FROM assignments;")
d = cursorObj.fetchall()
await ctx.send(f'{d}')
@client.command
async def новаяучетнаязапись(ctx):
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute('create table if not exists assignments(id TEXT, rawexp INTEGER, date TEXT, exp INTEGER, level INTEGER)')
userdate = datetime.now()
userid = ctx.author.id
userrawexp = 0
userexp = 0
userlevel = 1
cursorObj.execute("INSERT INTO assignments VALUES(?, ?, ?, ?, ?);", (userid, userrawexp, userdate, userexp, userlevel))
con.commit()
@client.command()
async def аа(ctx, *, text):
await ctx.send(f'aa')
@client.command()
async def время(ctx):
now = datetime.now()
s = now.time()
await ctx.send(f'{s}')
@client.command()
async def тестсинтаксиса(ctx):
await ctx.send(f"```lol```")
@client.command()
async def примитивныйкалькулятор(ctx,arg1,arg2,arg3):
a = arg1
b = arg2
c = arg3
if (b) == "*" :
a = int(a) * int(c)
else:
if (b) == "+":
a = int(a) + int(c)
else:
if (b) == "-":
a = int(a) + int(c)
else:
if (b) == "/":
a = int(a) / int(c)
await ctx.send(f'{int(a)}')
client.run('Token')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question