如何在python discord bot中标记成员

时间:2019-12-01 16:23:13

标签: python discord

所以我在python中有这个调平系统,但是我想添加一个@tag,所以该机器人提到了进行调平的成员...但是我不知道该怎么做。我已经尝试了多种方法,但是它们都不起作用。

    from discord.ext import commands
import discord
import logging
import yaml
import sqlite3
import time
import random
import re

logging.basicConfig(level=logging.INFO)


bot = commands.Bot(command_prefix='l>', description='Hype Levels')

def threshold(n):
    level_threshold = 5*(n**2)+50*n+100
    return level_threshold

@bot.command(pass_context=True)
async def rank(ctx):

    try:
        _, member = (ctx.message.content).split(' ', 1)
        member = re.sub("[^0-9]", "", member)
    except:
        member = ctx.message.author.id

    db = sqlite3.connect('users.db')
    c = db.cursor()

    c.execute('SELECT user.*, (SELECT count(*) FROM users AS members WHERE members.rawexp > user.rawexp) as Rank FROM users AS user WHERE id = ?',
              (ctx.message.author.id, ))

    user = c.fetchone()
    db.close()

    rank = str(user[6] + 1)

    embed = discord.Embed(title='{}\'s Information'.format(ctx.message.author.name)) \
            .set_thumbnail(url=ctx.message.author.avatar_url) \
            .add_field(name='Rank', value='#' + rank) \
            .add_field(name='Level', value=user[2]) \
            .add_field(name='EXP', value='{}/{}'.format(user[3], threshold(user[2]))) \
            .add_field(name='Raw EXP', value=user[4]) \

    await ctx.send(embed=embed)

@bot.event
async def on_message(message):

    if message.author == bot.user:
        return
    if message.author.bot:
        return

    if message.content.startswith('l>'):
        await bot.process_commands(message)
        return

    db = sqlite3.connect('users.db')
    c = db.cursor()

    c.execute('SELECT * FROM users WHERE id= ?', (message.author.id,))
    user = c.fetchone()

    if user is None:
        await message.channel.send('Looks like you\'re new! Welcome to level 1. Initializing player...')
        c.execute('INSERT INTO users(id, name, level, exp, rawexp, time) VALUES(?,?,?,?,?,?)', (message.author.id, message.author.name, 1, 0, 0, time.time()))
        db.commit()
        db.close()
        return

    if message.author.name != user[1]:
        c.execute('UPDATE users SET name = ? WHERE id= ?', (message.author.name, message.author.id))

    if (time.time() - user[5]) > 60:
        addedexp = random.randint(10, 25)
        exp = user[3] + addedexp
        rawexp = user[4] + addedexp
        c.execute('UPDATE users SET exp = ?, rawexp = ?, name = ?, time = ? WHERE id= ?', (exp, rawexp, message.author.name, time.time(), message.author.id))

        if (exp > threshold(user[2])):
            level = user[2] + 1
            c.execute('UPDATE users SET exp = ?, level = ? WHERE id= ?', (0, level, message.author.id))
            await message.channel.send('Wowza! You leveled up! Your level is now **{}**.'.format(level))


    db.commit()
    db.close()

    await bot.process_commands(message)

@bot.event
async def on_ready():
    print("HypeLevels V1.1")
    print("Made by: Garbodonk#6347")
    print("Bot Name: " + str(bot.user.name))
    print("Bot ID: " + str(bot.user.id))
    await bot.change_presence(activity=discord.Streaming(name='some HYPE', url="https://www.twitch.tv/hypepixelbot", type=1))

    #Initialize database.
    db = sqlite3.connect('users.db')
    c = db.cursor()
    c.execute('CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, level INT, exp INTEGER, rawexp INTEGER, time REAL)')
    db.commit()

bot.run('Token')

但是我想在人们升级时添加一个@tag,以便他们被标记

if (exp > threshold(user[2])):
            level = user[2] + 1
            c.execute('UPDATE users SET exp = ?, level = ? WHERE id= ?', (0, level, message.author.id))
            await message.channel.send('Wowza! You leveled up! Your level is now **{}**.'.format(level))

谁知道该怎么做?要问我问题或其他问题,只需加我不和谐即可:Garbodonk#6347

1 个答案:

答案 0 :(得分:0)

async def command(ctx, member : discord.Member, *, reason=None) :
         await ctx.send(f" can place text here if you want {member.mention} can place text here as well")```

I hope this helps.