1
1
1AAAhelpmeAAA12022-03-12 16:04:49
Python
1AAAhelpmeAAA1, 2022-03-12 16:04:49

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

error text
discord.ext.commands.errors.CommandNotFound: Command "new account" is not
found
. the rest of the functions are working properly.

here is my code:
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

1 answer(s)
S
shurshur, 2022-03-12
@1AAAhelpmeAAA1

By default, Discord does not send on_member_join events, for this you need to explicitly enable member intents.

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question