user13415893
user13415893

Reputation:

name 'user' is not defined mentions

I am trying to get when a user pings another user! Get the users mention and add a point into the database! Although User is not defined! And cant register any points for the user getting pinged!

import sqlite3
from discord.ext import commands

client = commands.Bot(command_prefix = "ping")

POINTS = 0

@client.event
async def on_ready():
    db = sqlite3.connect("ping.sqlite")
    cursor = db.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS main(
        user_id TEXT,
        points INTERGER
        )
        ''')
    print("Looking for Pings!")

@client.event
async def on_message(message):
    USER = user.mention.id
    db = sqlite3.connect("ping.sqlite")
    cursor = db.cursor()
    cursor.execute(f"SELECT user_id FROM main WHERE user_id = {USER}")
    result = cursor.fetchone()
    if result is None:
        sql = ("INSERT INTO main(user_id, points) VALUES(?,?)")
        val = (USER, POINTS)
    elif result is not None:
        sql = ("UPDATE main SET channel_id = ? WHERE guild_id = ?")
        val = (ctx.guild.id, channel.id)
    cursor.execute(sql, val)
    db.commit()
    cursor.close()
    db.close()

client.run(TOKEN)

Upvotes: 0

Views: 62

Answers (1)

Manoj A
Manoj A

Reputation: 432

if len(message.mentions) > 0:
        all_pinged_users = [user for user in message.mentions]
        #it will give all pinged user object in all_pinged_users and you can do anything with that!
    else:
        #no user is pinged

you can use this simple if statement to find if any user is pinged in the message! and the message.mentions is in the type of list of all pinged users in that specific message you can also iterate through the message.mentions to get all pinged user

Upvotes: 1

Related Questions