B
B
Bezola2021-06-04 11:28:45
Python
Bezola, 2021-06-04 11:28:45

How to remove a role from a user using id in discord.py?

I want to remove roles from all recorded users in the

JSON database:

[{"author": 606893698198011913, "exp": 5, "hero": 0}], [{"author": 400230840417779712, "exp": 0, "hero": 0}]  # и т.д.


Tried to enter id:
def load_stats_data():
    with open("stats.json", "r") as read_file:
        data = json.load(read_file)
        return data

users_data = list(load_stats_data())  # Получаем json

        with open("stats.json", "w") as f:
            for user in users_data:
                role_1 = user['author'].guild.get_role(834011803486257174)  # Снятие ролей
                await user['author'].remove_roles(role_1)
            json.dump(users_data, f)

But it gives an error
AttributeError: 'int' object has no attribute 'guild'

Tried to write ctx.author instead of ctx.author.id in JSON:
@bot.command()  # Регистрация
async def reg(ctx):
    author = ctx.author
    users_data = list(json_checker.load_stats_data())  # получаем данные со stats.json
    with open("stats.json", "w") as f:
        users_data.append({'author': ctx.author, 'exp': 0, 'hero': 0})
        json.dump(users_data, f)

Error:
TypeError: Object of type Member is not JSON serializable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-06-04
@Bezola

Write down the id, get the user object by id, and then do what you need.

from discord.utils import get
user = get(bot.get_all_members(), id=user['author'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question