1
0
Fork 0

Switch invite to slash commands

This commit is contained in:
Ethanell 2021-02-03 15:14:58 +01:00
parent 1b5d8410ae
commit ba55cb7c45

View file

@ -1,12 +1,16 @@
import re import re
from discord import Embed, Member, Guild from discord import Embed, Member, Guild, Role, CategoryChannel
from discord.abc import GuildChannel
from discord.errors import Forbidden from discord.errors import Forbidden
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from discord_slash import cog_ext, SlashContext, SlashCommandOptionType
from discord_slash.utils import manage_commands
import db import db
from administrator.check import is_enabled from administrator import slash
from administrator.check import is_enabled, guild_only, has_permissions
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled from administrator.utils import event_is_enabled
@ -20,41 +24,48 @@ class Invite(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
self.invites = {} self.invites = {}
slash.get_cog_commands(self)
self.bot.loop.create_task(self.update_invites()) self.bot.loop.create_task(self.update_invites())
def description(self): def description(self):
return "Get role from a special invite link" return "Get role from a special invite link"
@commands.group("invite", pass_context=True) @cog_ext.cog_subcommand(base="invite", name="help", description="Help about invite")
@is_enabled() @is_enabled()
@commands.guild_only() @guild_only()
@commands.has_guild_permissions(administrator=True) @has_permissions(administrator=True)
async def invite(self, ctx: commands.Context): async def invite_help(self, ctx: SlashContext):
if ctx.invoked_subcommand is None:
await ctx.invoke(self.invite_help)
@invite.group("help", pass_context=True)
async def invite_help(self, ctx: commands.Context):
embed = Embed(title="Invite help") embed = Embed(title="Invite help")
embed.add_field(name="invite create <#channel> <@role>", value="Create a invite link to a role", inline=False) embed.add_field(name="invite create <#channel> <@role>", value="Create a invite link to a role", inline=False)
embed.add_field(name="invite delete <code>", value="Remove a invite", inline=False) embed.add_field(name="invite delete <code>", value="Remove a invite", inline=False)
await ctx.send(embed=embed) await ctx.send(embeds=[embed])
@invite.group("create", pass_context=True) @cog_ext.cog_subcommand(base="invite", name="create", description="Create a invite link to a role",
async def invite_add(self, ctx: commands.Context, channel: str, role: str): options=[
if not channel_mention_re.fullmatch(channel) or len(ctx.message.channel_mentions) != 1 or\ manage_commands.create_option("channel", "The channel to join",
not role_mention_re.fullmatch(role) or len(ctx.message.role_mentions) != 1: SlashCommandOptionType.CHANNEL, True),
manage_commands.create_option("role", "The role to give",
SlashCommandOptionType.ROLE, True)
])
@is_enabled()
@guild_only()
@has_permissions(administrator=True)
async def invite_add(self, ctx: SlashContext, channel: GuildChannel, role: Role):
if isinstance(channel, CategoryChannel):
raise BadArgument() raise BadArgument()
inv = await channel.create_invite()
inv = await ctx.message.channel_mentions[0].create_invite()
s = db.Session() s = db.Session()
s.add(db.InviteRole(ctx.guild.id, inv.code, ctx.message.role_mentions[0].id)) s.add(db.InviteRole(ctx.guild.id, inv.code, role.id))
s.commit() s.commit()
s.close() s.close()
await ctx.send(f"Invite created: `{inv.url}`") await ctx.send(content=f"Invite created: `{inv.url}`")
@invite.group("delete", pass_context=True) @cog_ext.cog_subcommand(base="invite", name="delete", description="Remove a invite", options=[
async def invite_delete(self, ctx: commands.Context, code: str): manage_commands.create_option("code", "The invitation code", SlashCommandOptionType.STRING, True)])
@is_enabled()
@guild_only()
@has_permissions(administrator=True)
async def invite_delete(self, ctx: SlashContext, code: str):
inv = next(filter(lambda i: i.code == code, await ctx.guild.invites()), None) inv = next(filter(lambda i: i.code == code, await ctx.guild.invites()), None)
if not inv: if not inv:
raise BadArgument() raise BadArgument()
@ -68,7 +79,7 @@ class Invite(commands.Cog):
s.commit() s.commit()
s.close() s.close()
await inv.delete() await inv.delete()
await ctx.message.add_reaction("\U0001f44d") await ctx.send(content="\U0001f44d")
async def update_invites(self): async def update_invites(self):
for g in self.bot.guilds: for g in self.bot.guilds: