EmoteManager/cogs/meta.py

85 lines
2.7 KiB
Python
Raw Normal View History

2020-05-12 23:55:08 +00:00
# © 20182020 io mintz <io@mintz.cc>
#
# Emote Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Emote Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Emote Manager. If not, see <https://www.gnu.org/licenses/>.
2018-07-31 23:02:44 +00:00
2018-08-10 13:01:15 +00:00
import contextlib
2018-07-31 23:02:44 +00:00
import discord
from discord.ext import commands
2019-03-14 00:19:14 +00:00
class Meta(commands.Cog):
# TODO does this need to be configurable?
INVITE_DURATION_SECONDS = 60 * 60 * 3
MAX_INVITE_USES = 5
2018-07-31 23:02:44 +00:00
def __init__(self, bot):
self.bot = bot
2020-07-07 00:15:47 +00:00
self.support_channel = None
self.task = bot.loop.create_task(self.cache_invite_channel())
2018-07-31 23:02:44 +00:00
2020-07-07 00:15:47 +00:00
def cog_unload(self):
self.task.cancel()
2018-07-31 23:02:44 +00:00
2020-07-07 00:15:47 +00:00
async def cache_invite_channel(self):
self.support_channel = ch = await self.bot.fetch_channel(self.bot.config['support_server_invite_channel'])
return ch
2018-07-31 23:02:44 +00:00
2018-08-10 13:01:15 +00:00
@commands.command()
async def support(self, context):
"""Directs you to the support server."""
2020-07-07 00:15:47 +00:00
ch = self.support_channel or await self.cache_invite_channel()
2020-04-18 22:15:23 +00:00
reason = f'Created for {context.author} (ID: {context.author.id})'
2020-07-07 00:15:47 +00:00
invite = await ch.create_invite(
max_age=self.INVITE_DURATION_SECONDS,
max_uses=self.MAX_INVITE_USES,
reason=reason,
)
2018-08-10 13:01:15 +00:00
try:
await context.author.send(f'Official support server invite: {invite}')
2018-08-10 13:01:15 +00:00
except discord.Forbidden:
with contextlib.suppress(discord.HTTPException):
2018-08-22 15:27:35 +00:00
await context.message.add_reaction(utils.SUCCESS_EMOJIS[True])
with contextlib.suppress(discord.HTTPException):
await context.send('Unable to send invite in DMs. Please allow DMs from server members.')
else:
try:
await context.message.add_reaction('📬')
except discord.HTTPException:
with contextlib.suppress(discord.HTTPException):
await context.send('📬')
2018-08-10 13:01:15 +00:00
2020-07-07 00:15:47 +00:00
@commands.command(aliases=['inv'])
async def invite(self, context):
"""Gives you a link to add me to your server."""
permissions = discord.Permissions()
permissions.update(**dict.fromkeys((
'read_messages',
'send_messages',
'add_reactions',
'external_emojis',
'manage_emojis',
'embed_links',
'attach_files',
), True))
await context.send('<%s>' % discord.utils.oauth_url(self.bot.user.id, permissions))
2018-07-31 23:02:44 +00:00
def setup(bot):
bot.add_cog(Meta(bot))
2018-08-10 13:01:15 +00:00
if not bot.config.get('support_server_invite_channel'):
2018-08-10 13:01:15 +00:00
bot.remove_command('support')