update/only allow admin to use commands

This commit is contained in:
SowinskiBraeden committed 2024-11-17 14:48:23 -08:00
1 parent 815f76db88
commit 45376d4627
2 files changed
+14 -5

No files matched your search

+12 -3
View File
@@ -2,22 +2,31 @@
from discord.ext import commands from discord.ext import commands
from discord import app_commands from discord import app_commands
from discord import Interaction from discord import Interaction
from discord import Member
class Event(app_commands.Group): class Event(app_commands.Group):
def __init__(self, bot: commands.Bot, name: str, description: str): def __init__(self, bot: commands.Bot, name: str, description: str):
super().__init__(name=name, description=description) super().__init__(name=name, description=description)
self.bot = bot self.bot = bot
def hasPermission(self, user: Member) -> bool:
for role in user.roles:
if role.id == self.bot.config.ADMIN:
return True
return False
@app_commands.command(name="start", description="Start an event leaderboard") @app_commands.command(name="start", description="Start an event leaderboard")
async def start(self, interaction: Interaction) -> None: async def start(self, interaction: Interaction) -> None:
if not self.hasPermission(interaction.user): await interaction.response.send_message("You don't have permission to use this command.", ephemeral=True)
if self.bot._running: await interaction.response.send_message("Event already started") if self.bot._running: await interaction.response.send_message("Event already started")
self.bot.scrape_file.start() self.bot.scrape_file.start()
self.bot._running = True self.bot._running = True
await interaction.response.send_message("Starting new event leaderboard...") await interaction.response.send_message("Starting new event leaderboard...", ephemeral=True)
@app_commands.command(name="stop", description="Stop an event leaderboard") @app_commands.command(name="stop", description="Stop an event leaderboard")
async def stop(self, interaction: Interaction) -> None: async def stop(self, interaction: Interaction) -> None:
if not self.bot._running: await interaction.response.send_message("Event already stopped") if not self.hasPermission(interaction.user): await interaction.response.send_message("You don't have permission to use this command.", ephemeral=True)
if not self.bot._running: await interaction.response.send_message("Event already stopped", ephemeral=True)
self.bot.scrape_file.stop() self.bot.scrape_file.stop()
# Reset trackers # Reset trackers
@@ -28,7 +37,7 @@ class Event(app_commands.Group):
self.bot.gamertags = [] self.bot.gamertags = []
self.bot.message_id = "" self.bot.message_id = ""
await interaction.response.send_message("Stopping event leaderboard...") await interaction.response.send_message("Stopping event leaderboard...", ephemeral=True)
async def setup(bot: commands.Bot) -> None: async def setup(bot: commands.Bot) -> None:
bot.tree.add_command(Event(bot, name="event", description="start or stop an event leaderboard")) bot.tree.add_command(Event(bot, name="event", description="start or stop an event leaderboard"))
+2 -2
View File
@@ -7,8 +7,8 @@ load_dotenv()
class Config: class Config:
TOKEN: str = os.getenv("DISCORD_TOKEN") TOKEN: str = os.getenv("DISCORD_TOKEN")
GUILD: str = os.getenv("DISCORD_GUILD") GUILD: str = os.getenv("DISCORD_GUILD")
ADMIN: str = os.getenv("ADMIN_ROLE") ADMIN: str = int(os.getenv("ADMIN_ROLE"))
WEBOOK: str = os.getenv("WEBHOOK") WEBHOOK: str = os.getenv("WEBHOOK")
SFTP_HOST: str = os.getenv("SFTP_HOST") SFTP_HOST: str = os.getenv("SFTP_HOST")
SFTP_PORT: int = int(os.getenv("SFTP_PORT")) SFTP_PORT: int = int(os.getenv("SFTP_PORT"))