1
0
Fork 0

Merge pull request #50 from flifloo/disable_extension

Disable extension support
This commit is contained in:
Ethanell 2020-11-05 15:17:41 +01:00
commit 8f53c708ee
14 changed files with 61 additions and 7 deletions

View file

@ -9,7 +9,7 @@ class ExtensionDisabled(commands.CheckFailure):
def is_enabled(): def is_enabled():
async def check(ctx: commands.Context): async def check(ctx: commands.Context):
if ctx.command.cog: if ctx.command.cog and ctx.guild:
s = db.Session() s = db.Session()
es = s.query(db.ExtensionState).get((ctx.command.cog.qualified_name, ctx.guild.id)) es = s.query(db.ExtensionState).get((ctx.command.cog.qualified_name, ctx.guild.id))
s.close() s.close()

View file

@ -2,8 +2,10 @@ from discord.ext import commands
from discord import Member, Embed, Forbidden from discord import Member, Embed, Forbidden
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator import db, config from administrator import db, config
from administrator.utils import event_is_enabled
def check_greetings_message_type(message_type): def check_greetings_message_type(message_type):
@ -23,6 +25,7 @@ class Greetings(commands.Cog):
return "Setup join and leave message" return "Setup join and leave message"
@commands.group("greetings", pass_context=True) @commands.group("greetings", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_guild=True) @commands.has_permissions(manage_guild=True)
async def greetings(self, ctx: commands.Context): async def greetings(self, ctx: commands.Context):
@ -84,6 +87,8 @@ class Greetings(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_join(self, member: Member): async def on_member_join(self, member: Member):
s = db.Session() s = db.Session()
if not event_is_enabled(self.qualified_name, member.guild.id, s):
return
m = s.query(db.Greetings).filter(db.Greetings.guild == member.guild.id).first() m = s.query(db.Greetings).filter(db.Greetings.guild == member.guild.id).first()
s.close() s.close()
if m and m.join_enable: if m and m.join_enable:
@ -96,6 +101,8 @@ class Greetings(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_remove(self, member: Member): async def on_member_remove(self, member: Member):
s = db.Session() s = db.Session()
if not event_is_enabled(self.qualified_name, member.guild.id, s):
return
m = s.query(db.Greetings).filter(db.Greetings.guild == member.guild.id).first() m = s.query(db.Greetings).filter(db.Greetings.guild == member.guild.id).first()
s.close() s.close()
if m and m.leave_enable: if m and m.leave_enable:

View file

@ -6,7 +6,9 @@ from discord.ext import commands
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
import db import db
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled
extension_name = "invite" extension_name = "invite"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -24,6 +26,7 @@ class Invite(commands.Cog):
return "Get role from a special invite link" return "Get role from a special invite link"
@commands.group("invite", pass_context=True) @commands.group("invite", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
@commands.has_guild_permissions(administrator=True) @commands.has_guild_permissions(administrator=True)
async def invite(self, ctx: commands.Context): async def invite(self, ctx: commands.Context):
@ -77,6 +80,8 @@ class Invite(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_join(self, member: Member): async def on_member_join(self, member: Member):
if not event_is_enabled(self.qualified_name, member.guild.id):
return
user_invites = await member.guild.invites() user_invites = await member.guild.invites()
for i in self.invites[member.guild.id]: for i in self.invites[member.guild.id]:
for ui in user_invites: for ui in user_invites:
@ -93,11 +98,15 @@ class Invite(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_invite_create(self, invite): async def on_invite_create(self, invite):
if not event_is_enabled(self.qualified_name, invite.guild.id):
return
self.invites[invite.guild.id] = await invite.guild.invites() self.invites[invite.guild.id] = await invite.guild.invites()
@commands.Cog.listener() @commands.Cog.listener()
async def on_invite_delete(self, invite): async def on_invite_delete(self, invite):
s = db.Session() s = db.Session()
if not event_is_enabled(self.qualified_name, invite.guild.id, s):
return
invite_role = s.query(db.InviteRole).get({"guild_id": invite.guild.id, "invite_code": invite.code}) invite_role = s.query(db.InviteRole).get({"guild_id": invite.guild.id, "invite_code": invite.code})
if invite_role: if invite_role:
s.delete(invite_role) s.delete(invite_role)

View file

@ -5,6 +5,7 @@ from discord.ext import commands
from discord.ext.commands import BadArgument, MissingPermissions from discord.ext.commands import BadArgument, MissingPermissions
import db import db
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
@ -23,6 +24,7 @@ class PCP(commands.Cog):
return "PCP Univ Lyon 1" return "PCP Univ Lyon 1"
@commands.group("pcp", pass_context=True) @commands.group("pcp", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
async def pcp(self, ctx: commands.Context): async def pcp(self, ctx: commands.Context):
group = ctx.message.content.replace(f"{ctx.prefix}{ctx.command} ", "").upper() group = ctx.message.content.replace(f"{ctx.prefix}{ctx.command} ", "").upper()

View file

@ -6,8 +6,9 @@ from discord import Embed, RawReactionActionEvent, RawMessageDeleteEvent, RawBul
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
import db import db
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled
extension_name = "poll" extension_name = "poll"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -25,6 +26,7 @@ class Poll(commands.Cog):
return "Create poll with a simple command" return "Create poll with a simple command"
@commands.group("poll", pass_context=True) @commands.group("poll", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
async def poll(self, ctx: commands.Context, name: str, *choices): async def poll(self, ctx: commands.Context, name: str, *choices):
if name == "help": if name == "help":
@ -67,9 +69,11 @@ class Poll(commands.Cog):
user = await self.bot.fetch_user(payload.user_id) user = await self.bot.fetch_user(payload.user_id)
else: else:
user = payload.member user = payload.member
if not user.bot: if not user.bot:
s = db.Session() s = db.Session()
if payload.guild_id and not event_is_enabled(self.qualified_name, payload.guild_id, s):
return
p = s.query(db.Polls).filter(db.Polls.message == payload.message_id).first() p = s.query(db.Polls).filter(db.Polls.message == payload.message_id).first()
if p: if p:
message = await self.bot.get_channel(p.channel).fetch_message(p.message) message = await self.bot.get_channel(p.channel).fetch_message(p.message)

View file

@ -2,9 +2,10 @@ from discord.ext import commands
from discord import Embed, Message from discord import Embed, Message
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator import db from administrator import db
from administrator.utils import event_is_enabled
extension_name = "presentation" extension_name = "presentation"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -18,6 +19,7 @@ class Presentation(commands.Cog):
return "Give role to user who make a presentation in a dedicated channel" return "Give role to user who make a presentation in a dedicated channel"
@commands.group("presentation", pass_context=True) @commands.group("presentation", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_guild=True) @commands.has_permissions(manage_guild=True)
async def presentation(self, ctx: commands.Context): async def presentation(self, ctx: commands.Context):
@ -63,6 +65,8 @@ class Presentation(commands.Cog):
async def on_message(self, message: Message): async def on_message(self, message: Message):
if message.guild is not None: if message.guild is not None:
s = db.Session() s = db.Session()
if not event_is_enabled(self.qualified_name, message.guild.id, s):
return
p = s.query(db.Presentation).filter(db.Presentation.guild == message.guild.id).first() p = s.query(db.Presentation).filter(db.Presentation.guild == message.guild.id).first()
s.close() s.close()
if p and p.channel == message.channel.id and p.role not in map(lambda x: x.id, message.author.roles): if p and p.channel == message.channel.id and p.role not in map(lambda x: x.id, message.author.roles):

View file

@ -3,8 +3,9 @@ from asyncio import sleep
from discord.ext import commands from discord.ext import commands
from discord import Embed, RawReactionActionEvent from discord import Embed, RawReactionActionEvent
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled
extension_name = "purge" extension_name = "purge"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -19,6 +20,7 @@ class Purge(commands.Cog):
return "Purge all messages between the command and the next add reaction" return "Purge all messages between the command and the next add reaction"
@commands.group("purge", pass_context=True) @commands.group("purge", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_messages=True) @commands.has_permissions(manage_messages=True)
async def purge(self, ctx: commands.Context): async def purge(self, ctx: commands.Context):
@ -45,6 +47,8 @@ class Purge(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent): async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
if payload.guild_id: if payload.guild_id:
if not event_is_enabled(self.qualified_name, payload.guild_id):
return
user = self.bot.get_user(payload.user_id) user = self.bot.get_user(payload.user_id)
message = await self.bot.get_guild(payload.guild_id).get_channel(payload.channel_id)\ message = await self.bot.get_guild(payload.guild_id).get_channel(payload.channel_id)\
.fetch_message(payload.message_id) .fetch_message(payload.message_id)

View file

@ -6,6 +6,7 @@ from discord import Embed
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from discord.ext import tasks from discord.ext import tasks
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator import db from administrator import db
from administrator.utils import time_pars, seconds_to_time_string from administrator.utils import time_pars, seconds_to_time_string
@ -22,6 +23,7 @@ class Reminders(commands.Cog, name="Reminder"):
return "Create and manage reminders" return "Create and manage reminders"
@commands.group("reminder", pass_context=True) @commands.group("reminder", pass_context=True)
@is_enabled()
async def reminder(self, ctx: commands.Context): async def reminder(self, ctx: commands.Context):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.invoke(self.reminder_help) await ctx.invoke(self.reminder_help)

View file

@ -7,8 +7,9 @@ from discord import Embed, RawReactionActionEvent, RawBulkMessageDeleteEvent, Ra
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from administrator import db from administrator import db
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled
extension_name = "rorec" extension_name = "rorec"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -40,6 +41,7 @@ class RoRec(commands.Cog):
await (await ctx.channel.fetch_message(ctx.message.id)).remove_reaction(emoji, self.bot.user) await (await ctx.channel.fetch_message(ctx.message.id)).remove_reaction(emoji, self.bot.user)
@commands.group("rorec", pass_context=True) @commands.group("rorec", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
@commands.has_permissions(manage_roles=True) @commands.has_permissions(manage_roles=True)
async def rorec(self, ctx: commands.Context): async def rorec(self, ctx: commands.Context):
@ -204,6 +206,8 @@ class RoRec(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent): async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
s = db.Session() s = db.Session()
if payload.guild_id and not event_is_enabled(self.qualified_name, payload.guild_id, s):
return
m = s.query(db.RoRec).filter(db.RoRec.message == payload.message_id).first() m = s.query(db.RoRec).filter(db.RoRec.message == payload.message_id).first()
s.close() s.close()
if m and payload.member.id != self.bot.user.id: if m and payload.member.id != self.bot.user.id:

View file

@ -2,8 +2,9 @@ from discord.ext import commands
from discord import Member, VoiceState, Embed, Reaction, Guild from discord import Member, VoiceState, Embed, Reaction, Guild
from discord.ext.commands import CommandNotFound from discord.ext.commands import CommandNotFound
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import event_is_enabled
extension_name = "speak" extension_name = "speak"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -25,6 +26,7 @@ class Speak(commands.Cog):
return "Speech manager" return "Speech manager"
@commands.group("speak", pass_context=True) @commands.group("speak", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
@commands.has_guild_permissions(mute_members=True) @commands.has_guild_permissions(mute_members=True)
async def speak(self, ctx: commands.Context): async def speak(self, ctx: commands.Context):
@ -76,6 +78,8 @@ class Speak(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_voice_state_update(self, member: Member, before: VoiceState, after: VoiceState): async def on_voice_state_update(self, member: Member, before: VoiceState, after: VoiceState):
if member.guild and not event_is_enabled(self.qualified_name, member.guild.id):
return
if self.voice_chan and self.strict and \ if self.voice_chan and self.strict and \
(before is None or before.channel is None or before.channel.id != self.voice_chan) and \ (before is None or before.channel is None or before.channel.id != self.voice_chan) and \
(after is not None and after.channel is not None and after.channel.id == self.voice_chan) and \ (after is not None and after.channel is not None and after.channel.id == self.voice_chan) and \
@ -92,6 +96,8 @@ class Speak(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_reaction_add(self, reaction: Reaction, user: Member): async def on_reaction_add(self, reaction: Reaction, user: Member):
if user.guild and not event_is_enabled(self.qualified_name, user.guild.id):
return
if not user.bot: if not user.bot:
if self.voice_message and reaction.message.id == self.voice_message.id: if self.voice_message and reaction.message.id == self.voice_message.id:
if str(reaction.emoji) == "\U0001f5e3": if str(reaction.emoji) == "\U0001f5e3":
@ -211,6 +217,8 @@ class Speak(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_reaction_remove(self, reaction: Reaction, user: Member): async def on_reaction_remove(self, reaction: Reaction, user: Member):
if user.guild and not event_is_enabled(self.qualified_name, user.guild.id):
return
if not user.bot: if not user.bot:
if self.voice_message and reaction.message.id == self.voice_message.id: if self.voice_message and reaction.message.id == self.voice_message.id:
if str(reaction.emoji) == "\U0001f5e3" and user.id in self.waiting and user.id != self.last_speaker: if str(reaction.emoji) == "\U0001f5e3" and user.id in self.waiting and user.id != self.last_speaker:

View file

@ -3,6 +3,7 @@ from urllib.parse import urlencode
from discord import Embed from discord import Embed
from discord.ext import commands from discord.ext import commands
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
@ -18,6 +19,7 @@ class TeX(commands.Cog):
return "Render TeX formula" return "Render TeX formula"
@commands.group("tex", pass_context=True) @commands.group("tex", pass_context=True)
@is_enabled()
async def tex(self, ctx: commands.Context): async def tex(self, ctx: commands.Context):
if ctx.message.content.count("`") == 2: if ctx.message.content.count("`") == 2:
start = ctx.message.content.find("`") start = ctx.message.content.find("`")

View file

@ -8,6 +8,7 @@ from discord.ext.commands import BadArgument
from feedparser import parse from feedparser import parse
import db import db
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
@ -25,6 +26,7 @@ class Tomuss(commands.Cog):
return "PCP Univ Lyon 1" return "PCP Univ Lyon 1"
@commands.group("tomuss", pass_context=True) @commands.group("tomuss", pass_context=True)
@is_enabled()
async def tomuss(self, ctx: commands.Context): async def tomuss(self, ctx: commands.Context):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.invoke(self.tomuss_help) await ctx.invoke(self.tomuss_help)

View file

@ -4,6 +4,7 @@ from discord import Embed, Member, Guild
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
@ -19,6 +20,7 @@ class Utils(commands.Cog):
return "Some tools" return "Some tools"
@commands.group("utils", pass_context=True) @commands.group("utils", pass_context=True)
@is_enabled()
async def utils(self, ctx: commands.Context): async def utils(self, ctx: commands.Context):
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.invoke(self.utils_help) await ctx.invoke(self.utils_help)
@ -50,12 +52,14 @@ class Utils(commands.Cog):
await ctx.send(f"```{e.__class__.__name__}: {e}```") await ctx.send(f"```{e.__class__.__name__}: {e}```")
@commands.group("ping", pass_context=True) @commands.group("ping", pass_context=True)
@is_enabled()
async def ping(self, ctx: commands.Context): async def ping(self, ctx: commands.Context):
start = datetime.now() start = datetime.now()
msg = await ctx.send(f"Discord WebSocket latency: `{round(self.bot.latency*1000)}ms`") msg = await ctx.send(f"Discord WebSocket latency: `{round(self.bot.latency*1000)}ms`")
await msg.edit(content=msg.content+"\n"+f"Bot latency: `{round((msg.created_at - start).microseconds/1000)}ms`") await msg.edit(content=msg.content+"\n"+f"Bot latency: `{round((msg.created_at - start).microseconds/1000)}ms`")
@commands.group("info", pass_context=True) @commands.group("info", pass_context=True)
@is_enabled()
async def info(self, ctx: commands.Context): async def info(self, ctx: commands.Context):
if len(ctx.message.mentions) > 1: if len(ctx.message.mentions) > 1:
raise BadArgument() raise BadArgument()

View file

@ -3,6 +3,7 @@ from discord.ext import commands
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from administrator import db from administrator import db
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator.utils import time_pars, seconds_to_time_string from administrator.utils import time_pars, seconds_to_time_string
@ -39,6 +40,7 @@ class Warn(commands.Cog):
return users[user] return users[user]
@commands.group("warn", pass_context=True) @commands.group("warn", pass_context=True)
@is_enabled()
@commands.guild_only() @commands.guild_only()
#@commands.has_permissions(manage_roles=True, kick_members=True, ban_members=True, mute_members=True) #@commands.has_permissions(manage_roles=True, kick_members=True, ban_members=True, mute_members=True)
async def warn(self, ctx: commands.Context): async def warn(self, ctx: commands.Context):