This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchecks.py
176 lines (130 loc) · 4.46 KB
/
checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import discord
from discord.ext import commands
import vaivora.db
def check_channel(kind: str):
"""Checks whether a channel is allowed to
interact with Wings of Vaivora.
Once a channel type has even one channel set,
commands will fail if run outside those channels.
If the guild is in a state where no channel can be used,
whether because the old channel was deleted, etc.,
the guild's `authorized` can run `$settings purge` to
reset the channel table anew.
Args:
kind (str): the type (name) of the channel
Returns:
bool: True if successful; False otherwise
Note that this means if no channels have registered,
*all* channels are valid.
"""
@commands.check
async def check(ctx):
vdb = vaivora.db.Database(ctx.guild.id)
chs = await vdb.get_channels(kind)
if chs and ctx.channel.id not in chs:
return False # silently ignore wrong channel
else: # in the case of `None` chs, all channels are valid
return True
return check
def check_role(lesser_role=None):
"""Checks whether the user is authorized to run a settings command.
In the default case (`lesser_role` is None), the author must be
authorized.
Args:
lesser_role (str, optional): use this role instead of authorized;
defaults to None
Returns:
bool: True if the user is authorized; False otherwise
"""
@commands.check
async def check(ctx):
vdb = vaivora.db.Database(ctx.guild.id)
users = await vdb.get_users('s-authorized')
if await iterate_roles(ctx.author, users):
return True
users = await vdb.get_users('authorized')
if await iterate_roles(ctx.author, users):
return True
# for now, just use the default member role
if lesser_role:
users = await vdb.get_users('member')
else:
await ctx.send(
f"""{ctx.author.mention}
You are not authorized to do this!
"""
)
return False
if await iterate_roles(ctx.author, users):
return True
await ctx.send(
f"""{ctx.author.mention}
You are not authorized to do this!
"""
)
return False
return check
async def iterate_roles(author, users: list):
"""Iterates through the author's Discord roles.
Checks whether any of the author's Discord roles or
the author's id itself are in a list of
authorized `users`.
Called by `check_roles`.
Args:
author (discord.Member): the command user
users (list): users to iterate through
Returns:
bool: True if author is authorized; False otherwise
"""
if users and author.id in users:
return True
elif author.roles:
for role in author.roles:
try:
if role.id in users:
return True
except:
pass
return False
def only_in_guild():
"""Checks whether the command was called in a Discord guild.
If the command was not sent in a guild, e.g. DM,
the command is not run.
Returns:
bool: True if guild; False otherwise
"""
@commands.check
async def check(ctx):
if ctx.guild == None: # not a guild
await ctx.send(
'This command is not available in Direct Messages.'
)
return False
return True
return check
def has_channel_mentions():
"""Checks whether a command has channel mentions. How creative
Returns:
bool: True if message has channel mentions; False otherwise
"""
@commands.check
async def check(ctx):
if not ctx.message.channel_mentions: # not a guild
await ctx.send(
f"""{ctx.author.mention}
Too few or many arguments for `$settings`.
Usage: """
'`$settings set channel <type> <#channel> [<#channel> ...]`'
)
return False
return True
return check
async def is_bot_owner(user: discord.User, bot):
"""NOT A DECORATOR CHECK! Checks whether `user` owns `bot`.
Args:
user (discord.User): the user to check
bot: the bot itself
Returns:
bool: True if the user is the owner; False otherwise
"""
return user == (await bot.application_info()).owner