|
| 1 | +''' |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2017 verixx |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +
|
| 24 | +''' |
| 25 | + |
| 26 | +import discord |
| 27 | +from discord.ext import commands |
| 28 | +import datetime |
| 29 | + |
| 30 | +class Modmail(commands.Bot): |
| 31 | + def __init__(self, token): |
| 32 | + super().__init__(command_prefix='?') |
| 33 | + self.uptime = datetime.datetime.utcnow() |
| 34 | + self.add_commands() |
| 35 | + |
| 36 | + def add_commands(self): |
| 37 | + '''Adds commands automatically''' |
| 38 | + for attr in dir(self): |
| 39 | + cmd = getattr(self, attr) |
| 40 | + if isinstance(cmd, commands.Command): |
| 41 | + self.add_command(cmd) |
| 42 | + |
| 43 | + @property |
| 44 | + def token(self): |
| 45 | + '''Returns your token wherever it is''' |
| 46 | + with open('data/config.json') as f: |
| 47 | + config = json.load(f) |
| 48 | + if config.get('TOKEN') == "your_token_here": |
| 49 | + if not os.environ.get('TOKEN'): |
| 50 | + self.run_wizard() |
| 51 | + else: |
| 52 | + token = config.get('TOKEN').strip('\"') |
| 53 | + return os.environ.get('TOKEN') or token |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def run_wizard(): |
| 57 | + '''Wizard for first start''' |
| 58 | + print('------------------------------------------') |
| 59 | + token = input('Enter your token:\n> ') |
| 60 | + print('------------------------------------------') |
| 61 | + data = { |
| 62 | + "TOKEN" : token, |
| 63 | + } |
| 64 | + with open('data/config.json','w') as f: |
| 65 | + f.write(json.dumps(data, indent=4)) |
| 66 | + print('------------------------------------------') |
| 67 | + print('Restarting...') |
| 68 | + print('------------------------------------------') |
| 69 | + os.execv(sys.executable, ['python'] + sys.argv) |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def init(bot, token=None): |
| 73 | + '''Starts the actual bot''' |
| 74 | + selfbot = bot() |
| 75 | + safe_token = token or selfbot.token.strip('\"') |
| 76 | + try: |
| 77 | + selfbot.run(safe_token, bot=False, reconnect=True) |
| 78 | + except Exception as e: |
| 79 | + print(e) |
| 80 | + |
| 81 | + async def on_connect(self): |
| 82 | + print('---------------') |
| 83 | + print('Modmail connected!') |
| 84 | + |
| 85 | + async def on_ready(self): |
| 86 | + '''Bot startup, sets uptime.''' |
| 87 | + if not hasattr(self, 'uptime'): |
| 88 | + self.uptime = datetime.datetime.utcnow() |
| 89 | + print(textwrap.dedent(f''' |
| 90 | + --------------- |
| 91 | + Client is ready! |
| 92 | + --------------- |
| 93 | + Author: verixx#7220 |
| 94 | + --------------- |
| 95 | + Logged in as: {self.user} |
| 96 | + User ID: {self.user.id} |
| 97 | + --------------- |
| 98 | + ''')) |
| 99 | + |
| 100 | + @commands.command() |
| 101 | + async def setup(self, ctx): |
| 102 | + '''Sets up a server for modmail''' |
| 103 | + if discord.utils.get(ctx.guild.categories, name='modmail'): |
| 104 | + return await ctx.send('This server is already set up.') |
| 105 | + overwrites = { |
| 106 | + ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False) |
| 107 | + } |
| 108 | + |
| 109 | + modrole = self.guess_modrole(ctx) |
| 110 | + if modrole: |
| 111 | + overwrites[modrole] = discord.PermissionOverwrite(read_messages=True) |
| 112 | + |
| 113 | + await ctx.guild.create_category(name='modmail', overwrites=overwrites) |
| 114 | + await ctx.send('Successfully set up server.') |
| 115 | + |
| 116 | + def guess_modrole(ctx): |
| 117 | + ''' |
| 118 | + Finds a role if it has the manage_guild |
| 119 | + permission or if it startswith `mod` |
| 120 | + ''' |
| 121 | + perm_check = lambda r: r.permissions.manage_guild |
| 122 | + loose_check = lambda r: r.name.lower.startswith('mod') |
| 123 | + perm = discord.utils.find(perm_check, ctx.guild.roles) |
| 124 | + loose = discord.utils.find(loose_check, ctx.guild.roles) |
| 125 | + return perm or loose |
| 126 | + |
| 127 | + async def process_modmail(self, message): |
| 128 | + pass |
| 129 | + |
| 130 | + async def on_message(self, message): |
| 131 | + self.process_commands(message) |
| 132 | + if message.channel.is_private: |
| 133 | + await self.process_modmail(message) |
| 134 | + |
| 135 | + |
0 commit comments