Files
payphone/payphone/cogs/mods/cog.py
T
2024-11-15 23:24:11 -03:00

140 lines
4.3 KiB
Python

import discord
from discord.ext import commands
from payphone.db import models
from payphone.bot import Payphone
from payphone.cogs import base
class Mods(base.Cog):
"""Administration in Payphone."""
def __init__(self, bot: Payphone) -> None:
"""Initializes the Calls cog.
Args:
bot (Payphone): The bot instance.
"""
self.bot = bot
@commands.group(invoke_without_command=True)
@commands.guild_only()
@commands.has_guild_permissions(manage_guild=True)
async def config(
self, ctx: commands.Context, setting: str = "", value: str | None = None
) -> None:
"""
Set up Payphone for the current server.
Settings:
- `prefix`: Set a custom prefix.
- `enable`: Enable a (or all) channel(s) to run commands in it. Non-enabled channels will pass to be disabled.
- `disable`: Disables a channel to run commands in it. Non-disabled channels will stay enabled by default.
You can use `{prefix}help config <setting>` to get help about a specific configuration.
"""
await ctx.send_help("config")
@config.command(extras={"help_name": "Prefix"})
@commands.guild_only()
@commands.has_guild_permissions(manage_guild=True)
async def prefix(self, ctx: commands.Context, prefix: str = "p.") -> None:
"""
Set a custom bot prefix.
Options:
- `<prefix>`: The new custom prefix.
Permissions:
- Manage Server
"""
if len(prefix) > 10:
return await ctx.send(
"❌ The prefix must not be longer than 10 characters."
)
server, _ = await models.Server.update_or_create(
id=ctx.guild.id, defaults={"name": ctx.guild.name}
)
await server.set_prefix(self.bot.redis, prefix)
await ctx.send(f"✅ The prefix was set to `{prefix}`!")
@config.command(extras={"help_name": "Enable Channel"})
@commands.guild_only()
@commands.has_guild_permissions(manage_channels=True)
async def enable(
self, ctx: commands.Context, channel: discord.TextChannel | str | None
) -> None:
"""
Enable a channel to run commands in it. Non-enabled channels will pass to be disabled.
Options:
- `[<channel>]`: The channel to enable or "all". If no channel is specified, the current channel will be enabled.
Permissions:
- Manage Channels
"""
server_id = ctx.guild.id
channel = channel or ctx.channel
if isinstance(channel, str) and channel.lower() == "all":
await models.Channel.filter(server_id=server_id).update(is_enabled=None)
await ctx.bot.redis.delete(f"channels:{server_id}")
await ctx.send(
"✅ All channels were enabled! You can now run commands in any channel."
)
else:
await models.Channel.update_or_create(
id=channel.id, server_id=server_id, is_enabled=True
)
await ctx.bot.redis.hset(f"channels:{server_id}", channel.id, "1")
await ctx.send(
f"✅ {channel.mention} was enabled! Other non-enabled channels were disabled."
)
@config.command(extras={"help_name": "Disable Channel"})
@commands.guild_only()
@commands.has_guild_permissions(manage_channels=True)
async def disable(
self, ctx: commands.Context, channel: discord.TextChannel | None
) -> None:
"""
Disables a channel to run commands in it. Non-disabled channels will stay enabled by default.
Options:
- `[<channel>]`: The channel to disable. If no channel is specified, the current channel will be disabled.
Permissions:
- Manage Channels
"""
channel = channel or ctx.channel
await models.Channel.update_or_create(
id=channel.id, server_id=ctx.guild.id, is_enabled=False
)
await ctx.bot.redis.hset(f"channels:{ctx.guild.id}", channel.id, "0")
await ctx.send(
f"✅ {channel.mention} was disabled. Commands cannot be ran in this channel anymore."
)
def setup(bot: Payphone) -> None:
"""Loads the Mods cog into the bot.
Args:
bot (Payphone): The bot instance.
"""
bot.add_cog(Mods(bot))