Skip to content

Commit 1d6673e

Browse files
committed
Revamp for overwrites
1 parent 2839acd commit 1d6673e

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

bot.py

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,24 @@
2525

2626
import discord
2727
from discord.ext import commands
28+
import asyncio
29+
import aiohttp
2830
import datetime
31+
import psutil
32+
import time
33+
import json
34+
import sys
35+
import os
36+
import re
37+
import textwrap
2938

3039
class Modmail(commands.Bot):
31-
def __init__(self, token):
32-
super().__init__(command_prefix='?')
40+
def __init__(self):
41+
super().__init__(command_prefix='m.')
3342
self.uptime = datetime.datetime.utcnow()
34-
self.add_commands()
43+
self._add_commands()
3544

36-
def add_commands(self):
45+
def _add_commands(self):
3746
'''Adds commands automatically'''
3847
for attr in dir(self):
3948
cmd = getattr(self, attr)
@@ -43,13 +52,16 @@ def add_commands(self):
4352
@property
4453
def token(self):
4554
'''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('\"')
55+
try:
56+
with open('data/config.json') as f:
57+
config = json.load(f)
58+
if config.get('TOKEN') == "your_token_here":
59+
if not os.environ.get('TOKEN'):
60+
self.run_wizard()
61+
else:
62+
token = config.get('TOKEN').strip('\"')
63+
except FileNotFoundError:
64+
token = None
5365
return os.environ.get('TOKEN') or token
5466

5567
@staticmethod
@@ -72,9 +84,12 @@ def run_wizard():
7284
def init(bot, token=None):
7385
'''Starts the actual bot'''
7486
selfbot = bot()
75-
safe_token = token or selfbot.token.strip('\"')
87+
if token:
88+
to_use = token.strip('"')
89+
else:
90+
to_use = selfbot.token.strip('"')
7691
try:
77-
selfbot.run(safe_token, bot=False, reconnect=True)
92+
selfbot.run(to_use, reconnect=True)
7893
except Exception as e:
7994
print(e)
8095

@@ -97,39 +112,52 @@ async def on_ready(self):
97112
---------------
98113
'''))
99114

115+
def overwrites(self, ctx):
116+
'''Permision overwrites for the guild.'''
117+
overwrites = {
118+
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False)
119+
}
120+
121+
for role in self.guess_modroles(ctx):
122+
overwrites[role] = discord.PermissionOverwrite(read_messages=True)
123+
124+
return overwrites
125+
100126
@commands.command()
127+
@commands.has_permissions(administrator=True)
101128
async def setup(self, ctx):
102129
'''Sets up a server for modmail'''
103130
if discord.utils.get(ctx.guild.categories, name='modmail'):
104131
return await ctx.send('This server is already set up.')
105-
overwrites = {
106-
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False)
107-
}
108132

109-
modrole = self.guess_modrole(ctx)
110-
if modrole:
111-
overwrites[modrole] = discord.PermissionOverwrite(read_messages=True)
112133

113-
await ctx.guild.create_category(name='modmail', overwrites=overwrites)
134+
categ = await ctx.guild.create_category(name='modmail', overwrites=self.overwrites(ctx))
135+
await categ.edit(position=0)
114136
await ctx.send('Successfully set up server.')
115137

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
138+
@commands.command()
139+
async def ping(self, ctx):
140+
"""Pong! Returns your websocket latency."""
141+
em = discord.Embed()
142+
em.title ='Pong! Websocket Latency:'
143+
em.description = f'{self.ws.latency * 1000:.4f} ms'
144+
em.color = 0x00FF00
145+
await ctx.send(embed=em)
146+
147+
def guess_modroles(self, ctx):
148+
'''Finds roles if it has the manage_guild perm'''
149+
for role in ctx.guild.roles:
150+
if role.permissions.manage_guild:
151+
yield role
152+
126153

127154
async def process_modmail(self, message):
128155
pass
129156

130157
async def on_message(self, message):
131-
self.process_commands(message)
132-
if message.channel.is_private:
158+
await self.process_commands(message)
159+
if isinstance(message.channel, discord.DMChannel):
133160
await self.process_modmail(message)
134161

135-
162+
if __name__ == '__main__':
163+
Modmail.init('token')

0 commit comments

Comments
 (0)