25
25
26
26
import discord
27
27
from discord .ext import commands
28
+ import asyncio
29
+ import aiohttp
28
30
import datetime
31
+ import psutil
32
+ import time
33
+ import json
34
+ import sys
35
+ import os
36
+ import re
37
+ import textwrap
29
38
30
39
class Modmail (commands .Bot ):
31
- def __init__ (self , token ):
32
- super ().__init__ (command_prefix = '? ' )
40
+ def __init__ (self ):
41
+ super ().__init__ (command_prefix = 'm. ' )
33
42
self .uptime = datetime .datetime .utcnow ()
34
- self .add_commands ()
43
+ self ._add_commands ()
35
44
36
- def add_commands (self ):
45
+ def _add_commands (self ):
37
46
'''Adds commands automatically'''
38
47
for attr in dir (self ):
39
48
cmd = getattr (self , attr )
@@ -43,13 +52,16 @@ def add_commands(self):
43
52
@property
44
53
def token (self ):
45
54
'''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
53
65
return os .environ .get ('TOKEN' ) or token
54
66
55
67
@staticmethod
@@ -72,9 +84,12 @@ def run_wizard():
72
84
def init (bot , token = None ):
73
85
'''Starts the actual bot'''
74
86
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 ('"' )
76
91
try :
77
- selfbot .run (safe_token , bot = False , reconnect = True )
92
+ selfbot .run (to_use , reconnect = True )
78
93
except Exception as e :
79
94
print (e )
80
95
@@ -97,39 +112,52 @@ async def on_ready(self):
97
112
---------------
98
113
''' ))
99
114
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
+
100
126
@commands .command ()
127
+ @commands .has_permissions (administrator = True )
101
128
async def setup (self , ctx ):
102
129
'''Sets up a server for modmail'''
103
130
if discord .utils .get (ctx .guild .categories , name = 'modmail' ):
104
131
return await ctx .send ('This server is already set up.' )
105
- overwrites = {
106
- ctx .guild .default_role : discord .PermissionOverwrite (read_messages = False )
107
- }
108
132
109
- modrole = self .guess_modrole (ctx )
110
- if modrole :
111
- overwrites [modrole ] = discord .PermissionOverwrite (read_messages = True )
112
133
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 )
114
136
await ctx .send ('Successfully set up server.' )
115
137
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
+
126
153
127
154
async def process_modmail (self , message ):
128
155
pass
129
156
130
157
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 ) :
133
160
await self .process_modmail (message )
134
161
135
-
162
+ if __name__ == '__main__' :
163
+ Modmail .init ('token' )
0 commit comments