Ошибок не выдает, бот имеет права администратора, но роль не выдается.
Код:
import disnake
from disnake import ChannelType
from disnake.ext import commands
from disnake.errors import NotFound
import json
from config import settings
intents = disnake.Intents.default()
intents.message_content = True
intents.reactions = True
intents.members = True
bot = commands.Bot(command_prefix=settings['prefix'], intents=intents)
role_reactions_dict = {}
json_file = "role_reactions.json"
def load_reactions():
try:
with open(json_file, "r") as file:
return json.load(file)
except FileNotFoundError:
return {}
def save_reactions(reactions):
with open(json_file, "w") as file:
json.dump(reactions, file)
role_reactions_dict = load_reactions()
@bot.command()
async def add_role_reaction(ctx, message_id: int, emoji: str, role: disnake.Role):
message = None
for channel in ctx.guild.channels:
if channel.guild == ctx.guild and channel.type == ChannelType.text:
try:
message = await channel.fetch_message(message_id)
break
except NotFound:
pass
if not message:
await ctx.send("Сообщение с указанным ID не найдено.")
return
await message.add_reaction(emoji)
new_role = await ctx.guild.create_role(name=role.name, color=role.color)
role_reaction = {"message_id": message_id, "emoji": emoji, "role_id": new_role.id}
role_reaction["id"] = len(role_reactions_dict) + 1
role_reactions_dict[role_reaction["id"]] = role_reaction
save_reactions(role_reactions_dict)
await ctx.send(f"Роль '{new_role.name}' успешно создана за реакцией на сообщение с ID {message_id} emoji {emoji}")
@bot.event
async def on_raw_reaction_add(payload):
if not payload.guild_id:
return
for role_reaction in role_reactions_dict.values():
if (payload.message_id == role_reaction["message_id"] and
str(payload.emoji) == role_reaction["emoji"]):
guild = bot.get_guild(payload.guild_id)
if guild is not None:
role = guild.get_role(role_reaction["role_id"])
if role is not None:
member = guild.get_member(payload.user_id)
if member is not None:
await member.add_roles(role, reason="Role added by reaction.")
try:
await member.add_roles(role, reason="Role added by reaction.")
print(f"Добавлена роль {role} пользователю {member}")
except Exception as e:
print(f"Ошибка при добавлении роли {role} пользователю {member}: {e}")