简体   繁体   中英

Discord.py bot cannot ping, can someone show me what to do?

import discord
import random
from discord.ext import commands

TOKEN='MyToken'

client = commands.Bot(command_prefix = '$')

@client.event
async def on_ready():
    await client.change_presence(game=discord.Game(name='the love spreading!', type=3))
    print('## Startup Complete ##')

@client.command()
async def love(user1: discord.Member,        
user2: discord.Member):
    love=random.randint(0,100)
    await client.say(':heart:' + str(love) + '%:heart: \n ' + str(user1) + ' + '+ str(user2))

client.run(TOKEN)    

Heyy, it just says "@TheAlexGamer#7679" instead of a ping, why? (i have removed the part where it adds the @ to the string for stackoverflow)

Because when you write @TheAlexGamer#7679 on discord, your discord client change it automaticaly to <@id_of_alexgmer> .

For ping someone with a bot, use the member.mention attribut.

@client.command()
async def love(user1: discord.Member,        
user2: discord.Member):
    love=random.randint(0,100)
    await client.say(':heart:' + str(love) + '%:heart: \n ' + user1.mention + ' + '+ user2.mention)

client.run(TOKEN)    

you use their discord id to ping so you can use this code

@client.command()
async def love(ctx):
    await ctx.send(f":heart: <@{ctx.author.id}>")

it gets the person who sent the message id and then pings them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM