'my discord bot dosen't give role automaticly
I am trying to make a discord bot that give a role to a user according to his league of legend rank that I get by scraping a website that give information. I create different role on my server. Example if he is bronze the bot gonna put the user in the bronze role. the user need to put $rank 'username' and this work fine but it's just dosen't give the role I dont have any error. the code is in french but the part where it dosent work is in english. The probleme is in line 57-60 (the end of the code):
import discord
import os
from bs4 import BeautifulSoup
import requests
my_secret = os.environ['TOKEN']
def startbot():
global rank
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
member = message.author.id
if message.content.startswith('$rank'):
stringsplit = message.content.split()
nmb = len(stringsplit)
if nmb == 1:
await message.channel.send("aucun username détecté. Ecrire ($rank USERNAME)")
return
else:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 OPR/82.0.4227.58',
'accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
}
username = stringsplit[1]
name = "https://na.op.gg/summoner/userName=" + username
source = requests.get(name, headers = headers)
try:
soup = BeautifulSoup(source.text, "html.parser")
except:
await message.channel.send("cette utilisateur n'existe pas.")
return
rank = soup.find('div' , class_="TierRank")
try:
rank = rank.text
except:
await message.channel.send("cette utilisateur n'existe pas.")
return
if rank == "Unranked":
await message.channel.send("cette utilisateur est non classé.")
return
rank = rank.split()
rank = rank[0]
else:
if message.author == client.user:
return
await message.delete()
return
@client.event
async def on_message(member):
role = discord.utils.get(member.server.roles, name=rank)
await client.add_roles(member, role)
client.run(my_secret)
startbot()
Solution 1:[1]
For prefixed commands you should be using commands.Bot not discord.Client.
Prefixed Commands should be defined as such:
from discord.ext import commands
bot = commands.Bot(command_prefix=“=“)
@client.command()
async def cmd_name(ctx,*args):
…
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fishball Nooodles |
