+ Upload
This commit is contained in:
104
cogs/reaction_roles.py
Normal file
104
cogs/reaction_roles.py
Normal file
@@ -0,0 +1,104 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord import app_commands
|
||||
import sqlite3
|
||||
|
||||
class ReactionRoles(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
self.db_connection = sqlite3.connect("xalos_data.db")
|
||||
self.db_cursor = self.db_connection.cursor()
|
||||
self.db_cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS reaction_roles (
|
||||
message_id INTEGER,
|
||||
guild_id INTEGER,
|
||||
role_id INTEGER,
|
||||
emoji TEXT,
|
||||
PRIMARY KEY (message_id, emoji)
|
||||
)
|
||||
""")
|
||||
|
||||
@app_commands.group(name="reactionrole", description="Verwaltet Reaktions-Rollen.")
|
||||
@app_commands.checks.has_permissions(manage_roles=True)
|
||||
async def reactionrole(self, interaction: discord.Interaction):
|
||||
# This base command is not invoked directly
|
||||
pass
|
||||
|
||||
@reactionrole.command(name="add", description="Fügt eine neue Reaktions-Rolle zu einer Nachricht hinzu.")
|
||||
@app_commands.describe(
|
||||
message_id="Die ID der Nachricht, zu der die Rolle hinzugefügt werden soll.",
|
||||
emoji="Das Emoji, das als Reaktion verwendet wird.",
|
||||
role="Die Rolle, die vergeben werden soll."
|
||||
)
|
||||
async def add_reaction_role(self, interaction: discord.Interaction, message_id: str, emoji: str, role: discord.Role):
|
||||
"""Fügt eine neue Reaktions-Rolle hinzu."""
|
||||
try:
|
||||
# Check if the message exists
|
||||
message = await interaction.channel.fetch_message(int(message_id))
|
||||
except (discord.NotFound, ValueError):
|
||||
await interaction.response.send_message("Ungültige Nachrichten-ID. Bitte stelle sicher, dass die ID korrekt ist und der Befehl im selben Kanal wie die Nachricht ausgeführt wird.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Add the reaction to the message so users can see it
|
||||
try:
|
||||
await message.add_reaction(emoji)
|
||||
except discord.HTTPException:
|
||||
await interaction.response.send_message("Ungültiges Emoji. Ich konnte es nicht zur Nachricht hinzufügen.", ephemeral=True)
|
||||
return
|
||||
|
||||
# Save to database
|
||||
try:
|
||||
self.db_cursor.execute(
|
||||
"INSERT INTO reaction_roles (message_id, guild_id, role_id, emoji) VALUES (?, ?, ?, ?)",
|
||||
(message.id, interaction.guild.id, role.id, emoji)
|
||||
)
|
||||
self.db_connection.commit()
|
||||
except sqlite3.IntegrityError:
|
||||
await interaction.response.send_message("Diese Emoji-Rollen-Kombination existiert bereits für diese Nachricht.", ephemeral=True)
|
||||
return
|
||||
|
||||
await interaction.response.send_message(f"✅ Reaktions-Rolle hinzugefügt: Wenn jemand mit {emoji} auf die Nachricht reagiert, erhält er/sie die Rolle '{role.name}'.", ephemeral=True)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
|
||||
# Ignore reactions from bots
|
||||
if payload.member.bot:
|
||||
return
|
||||
|
||||
# Check if this reaction is for a configured reaction role
|
||||
self.db_cursor.execute("SELECT role_id FROM reaction_roles WHERE message_id = ? AND emoji = ?", (payload.message_id, str(payload.emoji)))
|
||||
result = self.db_cursor.fetchone()
|
||||
|
||||
if result:
|
||||
role_id = result[0]
|
||||
guild = self.bot.get_guild(payload.guild_id)
|
||||
role = guild.get_role(role_id)
|
||||
|
||||
if role:
|
||||
try:
|
||||
await payload.member.add_roles(role, reason="Reaction Role")
|
||||
except discord.Forbidden:
|
||||
# Bot has insufficient permissions
|
||||
pass
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
|
||||
# Check if this reaction is for a configured reaction role
|
||||
self.db_cursor.execute("SELECT role_id FROM reaction_roles WHERE message_id = ? AND emoji = ?", (payload.message_id, str(payload.emoji)))
|
||||
result = self.db_cursor.fetchone()
|
||||
|
||||
if result:
|
||||
role_id = result[0]
|
||||
guild = self.bot.get_guild(payload.guild_id)
|
||||
member = guild.get_member(payload.user_id)
|
||||
role = guild.get_role(role_id)
|
||||
|
||||
if guild and member and role:
|
||||
try:
|
||||
await member.remove_roles(role, reason="Reaction Role")
|
||||
except discord.Forbidden:
|
||||
# Bot has insufficient permissions
|
||||
pass
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(ReactionRoles(bot))
|
Reference in New Issue
Block a user