1
+ from discord .ext import commands
2
+ from .utils import config , checks
3
+ from collections import Counter
4
+ import re
5
+ import discord
6
+ import asyncio
7
+
8
+ class Mod :
9
+ """Moderation related commands."""
10
+
11
+ def __init__ (self , bot ):
12
+ self .bot = bot
13
+ self .config = config .Config ('mod.json' , loop = bot .loop )
14
+
15
+ def bot_user (self , message ):
16
+ return message .server .me if message .channel .is_private else self .bot .user
17
+
18
+ @commands .group (pass_context = True , no_pm = True , hidden = True )
19
+ @checks .admin_or_permissions (manage_channels = True )
20
+ async def ignore (self , ctx ):
21
+ """Handles the bot's ignore lists.
22
+ To use these commands, you must have the Bot Admin role or have
23
+ Manage Channels permissions. These commands are not allowed to be used
24
+ in a private message context.
25
+ Users with Manage Roles or Bot Admin role can still invoke the bot
26
+ in ignored channels.
27
+ """
28
+ if ctx .invoked_subcommand is None :
29
+ await self .bot .say ('Invalid subcommand passed: {0.subcommand_passed}' .format (ctx ))
30
+
31
+ @ignore .command (name = 'list' , pass_context = True )
32
+ async def ignore_list (self , ctx ):
33
+ """Tells you what channels are currently ignored in this server."""
34
+
35
+ ignored = self .config .get ('ignored' , [])
36
+ channel_ids = set (c .id for c in ctx .message .server .channels )
37
+ result = []
38
+ for channel in ignored :
39
+ if channel in channel_ids :
40
+ result .append ('<#{}>' .format (channel ))
41
+
42
+ if result :
43
+ await self .bot .say ('The following channels are ignored:\n \n {}' .format (', ' .join (result )))
44
+ else :
45
+ await self .bot .say ('I am not ignoring any channels here.' )
46
+
47
+ @ignore .command (name = 'channel' , pass_context = True )
48
+ async def channel_cmd (self , ctx , * , channel : discord .Channel = None ):
49
+ """Ignores a specific channel from being processed.
50
+ If no channel is specified, the current channel is ignored.
51
+ If a channel is ignored then the bot does not process commands in that
52
+ channel until it is unignored.
53
+ """
54
+
55
+ if channel is None :
56
+ channel = ctx .message .channel
57
+
58
+ ignored = self .config .get ('ignored' , [])
59
+ if channel .id in ignored :
60
+ await self .bot .say ('That channel is already ignored.' )
61
+ return
62
+
63
+ ignored .append (channel .id )
64
+ await self .config .put ('ignored' , ignored )
65
+ await self .bot .say ('\U0001f44c ' )
66
+
67
+ @ignore .command (name = 'all' , pass_context = True )
68
+ @checks .admin_or_permissions (manage_server = True )
69
+ async def _all (self , ctx ):
70
+ """Ignores every channel in the server from being processed.
71
+ This works by adding every channel that the server currently has into
72
+ the ignore list. If more channels are added then they will have to be
73
+ ignored by using the ignore command.
74
+ To use this command you must have Manage Server permissions along with
75
+ Manage Channels permissions. You could also have the Bot Admin role.
76
+ """
77
+
78
+ ignored = self .config .get ('ignored' , [])
79
+ channels = ctx .message .server .channels
80
+ ignored .extend (c .id for c in channels if c .type == discord .ChannelType .text )
81
+ await self .config .put ('ignored' , list (set (ignored ))) # make unique
82
+ await self .bot .say ('\U0001f44c ' )
83
+
84
+ @commands .command (pass_context = True , no_pm = True )
85
+ @checks .admin_or_permissions (manage_channels = True )
86
+ async def unignore (self , ctx , * , channel : discord .Channel = None ):
87
+ """Unignores a specific channel from being processed.
88
+ If no channel is specified, it unignores the current channel.
89
+ To use this command you must have the Manage Channels permission or have the
90
+ Bot Admin role.
91
+ """
92
+
93
+ if channel is None :
94
+ channel = ctx .message .channel
95
+
96
+ # a set is the proper data type for the ignore list
97
+ # however, JSON only supports arrays and objects not sets.
98
+ ignored = self .config .get ('ignored' , [])
99
+ try :
100
+ ignored .remove (channel .id )
101
+ except ValueError :
102
+ await self .bot .say ('Channel was not ignored in the first place.' )
103
+ else :
104
+ await self .bot .say ('\U0001f44c ' )
105
+
106
+ @commands .command (no_pm = True )
107
+ @checks .admin_or_permissions (manage_server = True )
108
+ async def plonk (self , * , member : discord .Member ):
109
+ """Bans a user from using the bot.
110
+ Note that this ban is **global**. So they are banned from
111
+ all servers that they access the bot with. So use this with
112
+ caution.
113
+ There is no way to bypass a plonk regardless of role or permissions.
114
+ The only person who cannot be plonked is the bot creator. So this
115
+ must be used with caution.
116
+ To use this command you must have the Manage Server permission
117
+ or have a Bot Admin role.
118
+ """
119
+
120
+ plonks = self .config .get ('plonks' , [])
121
+ if member .id in plonks :
122
+ await self .bot .say ('That user is already bot banned.' )
123
+ return
124
+
125
+ plonks .append (member .id )
126
+ await self .config .put ('plonks' , plonks )
127
+ await self .bot .say ('{0.name} has been banned from using the bot.' .format (member ))
128
+
129
+ @commands .command (no_pm = True )
130
+ @checks .admin_or_permissions (manage_server = True )
131
+ async def unplonk (self , * , member : discord .Member ):
132
+ """Unbans a user from using the bot.
133
+ To use this command you must have the Manage Server permission
134
+ or have a Bot Admin role.
135
+ """
136
+
137
+ plonks = self .config .get ('plonks' , [])
138
+
139
+ try :
140
+ plonks .remove (member .id )
141
+ except ValueError :
142
+ pass
143
+ else :
144
+ await self .config .put ('plonks' , plonks )
145
+ await self .bot .say ('{0.name} has been unbanned from using the bot.' .format (member ))
146
+
147
+ def setup (bot ):
148
+ bot .add_cog (Mod (bot ))
0 commit comments