@RED2190

Как сделать выдачу роли?

Бот не хочет выдавать роль и выдаёт ошибку в консоль:

AttributeError: 'User' object has no attribute 'add_roles'

Вот код:

@buttons.click
async def vac_b(ctx):
	ch = bot.get_channel(947195652079763536)
	mem = ctx.member
	me = ctx.id
	verif = discord.utils.get(ctx.guild.roles, id = 944610868140867616)

	ms = "**Вы успешно вакцинировались, добро пожаловать на сервер**"
	emb = discord.Embed(
		title = '**__ВЫ ВАКЦИНИРОВАНЫ!__**', 
		description = f"{ms}, {mem.mention}",
		color = discord.Color.red())


	await ch.send(embed = emb)
	await mem.add_roles(verif)

Библиотеки:

import discord
from discord.ext import commands
from discord_components import DiscordComponents, Button, ButtonStyle
from discord.ext import commands, tasks
from discord_components import *
from discord_buttons_plugin import  *
from discord.ext.commands import Bot
from discord import member
from discord.utils import get

intents = discord.Intents.all()
from discord.ext import commands, tasks
  • Вопрос задан
  • 330 просмотров
Решения вопроса 1
Vindicar
@Vindicar
RTFM!
Если почитать справку на гитхабе discord_buttons_plugin, можно найти следующее:
The ctx parameter is an instance of InteractionContext
Attributes:
.id int : The interaction ID.
.token str : The interaction token.
.message discord.Message : The message where buttons are present
.channel discord.TextChannel : The channel where buttons are present.
.guild discord.Guild : The guild where buttons are present.
.member discord.User : The user who clicked the button. Remember this is discord.User and not discord.Member

Т.е. название атрибута сбивает с толку, он содержит не ссылку на пользователя как участника сервера (Member), а на пользователя дискорда вообще (User). Нужно получить объект сервера (ctx.guild), и у него вызвать метод get_member(ctx.member.id), чтобы получить объект member, на котором можно будет вызывать метод add_roles().
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
menma_uzumakie
@menma_uzumakie
Тут все уже для вас описали
https://ru.stackoverflow.com/questions/1278781/%D0...
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы