I am trying to write a bot which fetches the data from mee6's API and gives moderator roles to the top 3 in the leaderboard. I got the userid of those people as well but now I am not able to put those user in the parameters and give those users the role.
I tried to do it using some events but I didn't find a suitable one to I did on message event. If you have any other solutions on that it would be very helpful.
I am a beginner Python user and I don't know how to efficiently use API or write good Python code.
Code:
import requests
import json
import discord
from discord.ext import commands, tasks
from discord.utils import get
class applicable_members:
#Variables
member={}
mee6API = "https://mee6.xyz/api/plugins/levels/leaderboard/766213304141086731?limit=999&page=0"
#Gets the data from api
result = requests.get(mee6API).json()
#Gets specific data related to members
players = result.get("players")
#Loop to form dict of player and their xp
for player in players:
member[player.get("xp")] = player.get("id")
#List of user-id of people illegible to get Mod role
people = list(member.values())[:3]
class samrid_bot:
#Variables
token = "<TOKEN>"
role_name = "Mods"
peoples = applicable_members.people
#Declaring Prefix
client = commands.Bot(command_prefix=".", case_insensitive=True)
@client.event
async def on_ready():
print("Bot is ready")
ppl = peoples[1]
@tasks.loop(seconds=5.0, count=5)
async def addrole(person, role_n):
member = person
role = role_n
await client.add_roles(member, role)
addrole.start(ppl, role_name)
client.run(token)
Error:
Unhandled exception in internal background task 'add'.
Traceback (most recent call last):
File "C:\Users\samri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\tasks\__init__.py", line 101, in _loop
await self.coro(*args, **kwargs)
File "c:/Users/samri/OneDrive - Hoffman/Documents/Websites/Discord Bot/Samrids Bot/bot.py", line 52, in add
await client.add_roles(member, role)
AttributeError: 'Bot' object has no attribute 'add_roles'
Bot is ready