1

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

1 Answer 1

1

on_message is an event listener that only takes one parameter, message. https://discordpy.readthedocs.io/en/latest/api.html#discord.on_message

This executes whenever a message is sent, with the message argument being set to the message that was sent. You cannot modify it to take your own arguments, which is why that code fails.

edit: in order to repeat it, you would be better off using a tasks loop which discord.py provides https://discordpy.readthedocs.io/en/latest/ext/tasks/

Sign up to request clarification or add additional context in comments.

8 Comments

Ok any suggestions? What can i do to make it accept my arguments?
it depends where you want to do this, your code doesn't necessarily need to be inside a listener. So you could just do it outside or in on_ready.
Yeah thanks and how do i make it repeat, I need it to run every hour or so?
In that case instead of on_ready, just use a task loop discordpy.readthedocs.io/en/latest/ext/tasks, I'll edit my answer with it
Thanks you helped me a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.