2020-11-02 11:56:41 +01:00
|
|
|
import re
|
|
|
|
|
2021-02-03 15:14:58 +01:00
|
|
|
from discord import Embed, Member, Guild, Role, CategoryChannel
|
|
|
|
from discord.abc import GuildChannel
|
2020-11-02 11:56:41 +01:00
|
|
|
from discord.errors import Forbidden
|
|
|
|
from discord.ext import commands
|
|
|
|
from discord.ext.commands import BadArgument
|
2021-02-03 15:14:58 +01:00
|
|
|
from discord_slash import cog_ext, SlashContext, SlashCommandOptionType
|
|
|
|
from discord_slash.utils import manage_commands
|
2020-11-02 11:56:41 +01:00
|
|
|
|
|
|
|
import db
|
2021-02-03 15:14:58 +01:00
|
|
|
from administrator import slash
|
|
|
|
from administrator.check import is_enabled, guild_only, has_permissions
|
2020-11-02 11:56:41 +01:00
|
|
|
from administrator.logger import logger
|
2020-11-05 14:47:11 +01:00
|
|
|
from administrator.utils import event_is_enabled
|
2020-11-02 11:56:41 +01:00
|
|
|
|
|
|
|
extension_name = "invite"
|
|
|
|
logger = logger.getChild(extension_name)
|
|
|
|
role_mention_re = re.compile(r"<@&[0-9]+>")
|
|
|
|
channel_mention_re = re.compile(r"<#[0-9]+>")
|
|
|
|
|
|
|
|
|
|
|
|
class Invite(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.invites = {}
|
2021-02-03 15:14:58 +01:00
|
|
|
slash.get_cog_commands(self)
|
2020-11-02 11:56:41 +01:00
|
|
|
self.bot.loop.create_task(self.update_invites())
|
|
|
|
|
|
|
|
def description(self):
|
|
|
|
return "Get role from a special invite link"
|
|
|
|
|
2021-02-03 15:14:58 +01:00
|
|
|
@is_enabled()
|
|
|
|
@guild_only()
|
|
|
|
@has_permissions(administrator=True)
|
|
|
|
async def invite_add(self, ctx: SlashContext, channel: GuildChannel, role: Role):
|
|
|
|
if isinstance(channel, CategoryChannel):
|
2020-11-02 11:56:41 +01:00
|
|
|
raise BadArgument()
|
2021-02-03 15:14:58 +01:00
|
|
|
inv = await channel.create_invite()
|
2020-11-02 11:56:41 +01:00
|
|
|
s = db.Session()
|
2021-02-03 15:14:58 +01:00
|
|
|
s.add(db.InviteRole(ctx.guild.id, inv.code, role.id))
|
2020-11-02 11:56:41 +01:00
|
|
|
s.commit()
|
|
|
|
s.close()
|
2021-02-03 15:14:58 +01:00
|
|
|
await ctx.send(content=f"Invite created: `{inv.url}`")
|
2020-11-02 11:56:41 +01:00
|
|
|
|
2021-02-03 15:14:58 +01:00
|
|
|
@cog_ext.cog_subcommand(base="invite", name="delete", description="Remove a invite", options=[
|
|
|
|
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):
|
2020-11-02 11:56:41 +01:00
|
|
|
inv = next(filter(lambda i: i.code == code, await ctx.guild.invites()), None)
|
|
|
|
if not inv:
|
|
|
|
raise BadArgument()
|
|
|
|
|
|
|
|
s = db.Session()
|
|
|
|
invite_role = s.query(db.InviteRole).get({"guild_id": ctx.guild.id, "invite_code": code})
|
|
|
|
if not invite_role:
|
|
|
|
s.close()
|
|
|
|
raise BadArgument()
|
|
|
|
s.delete(invite_role)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
await inv.delete()
|
2021-02-03 15:14:58 +01:00
|
|
|
await ctx.send(content="\U0001f44d")
|
2020-11-02 11:56:41 +01:00
|
|
|
|
|
|
|
async def update_invites(self):
|
|
|
|
for g in self.bot.guilds:
|
|
|
|
self.invites[g.id] = await g.invites()
|
|
|
|
|
2020-11-02 18:18:19 +01:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_ready(self):
|
|
|
|
await self.update_invites()
|
|
|
|
|
2020-11-02 11:56:41 +01:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_member_join(self, member: Member):
|
2020-11-05 14:47:11 +01:00
|
|
|
if not event_is_enabled(self.qualified_name, member.guild.id):
|
|
|
|
return
|
2020-11-02 11:56:41 +01:00
|
|
|
user_invites = await member.guild.invites()
|
|
|
|
for i in self.invites[member.guild.id]:
|
|
|
|
for ui in user_invites:
|
|
|
|
if i.code == ui.code and i.uses < ui.uses:
|
|
|
|
s = db.Session()
|
|
|
|
invite_role = s.query(db.InviteRole).get({"guild_id": member.guild.id, "invite_code": i.code})
|
|
|
|
s.close()
|
|
|
|
if invite_role:
|
|
|
|
try:
|
|
|
|
await member.add_roles(member.guild.get_role(invite_role.role_id))
|
|
|
|
except Forbidden:
|
|
|
|
pass
|
|
|
|
self.invites[member.guild.id] = user_invites
|
|
|
|
|
2020-11-02 12:30:26 +01:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_invite_create(self, invite):
|
2020-11-05 14:47:11 +01:00
|
|
|
if not event_is_enabled(self.qualified_name, invite.guild.id):
|
|
|
|
return
|
2020-11-02 12:30:26 +01:00
|
|
|
self.invites[invite.guild.id] = await invite.guild.invites()
|
|
|
|
|
2020-11-02 11:56:41 +01:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_invite_delete(self, invite):
|
|
|
|
s = db.Session()
|
2020-11-05 14:47:11 +01:00
|
|
|
if not event_is_enabled(self.qualified_name, invite.guild.id, s):
|
|
|
|
return
|
2020-11-02 11:56:41 +01:00
|
|
|
invite_role = s.query(db.InviteRole).get({"guild_id": invite.guild.id, "invite_code": invite.code})
|
|
|
|
if invite_role:
|
|
|
|
s.delete(invite_role)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
2020-11-02 12:30:26 +01:00
|
|
|
self.invites[invite.guild.id] = await invite.guild.invites()
|
2020-11-02 11:56:41 +01:00
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_guild_join(self, guild: Guild):
|
|
|
|
self.invites[guild.id] = await guild.invites()
|
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_guild_remove(self, guild: Guild):
|
|
|
|
s = db.Session()
|
|
|
|
for g in s.query(db.InviteRole).filter(db.InviteRole.guild_id == guild.id).all():
|
|
|
|
s.delete(g)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
del self.invites[guild.id]
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
logger.info(f"Loading...")
|
|
|
|
try:
|
|
|
|
bot.add_cog(Invite(bot))
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error loading: {e}")
|
|
|
|
else:
|
|
|
|
logger.info(f"Load successful")
|
|
|
|
|
|
|
|
|
|
|
|
def teardown(bot):
|
|
|
|
logger.info(f"Unloading...")
|
|
|
|
try:
|
|
|
|
bot.remove_cog("Invite")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error unloading: {e}")
|
|
|
|
else:
|
|
|
|
logger.info(f"Unload successful")
|