This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebatebot.py
executable file
·238 lines (203 loc) · 10.4 KB
/
debatebot.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""A Discord bot for structuring Discord debates"""
import asyncio
import discord
from discord.ext.commands import has_permissions, bot_has_permissions, converter, check, has_role
import db
from config import Config
BOT = discord.ext.commands.Bot(command_prefix='d!')
def does_not_have_role(name):
def predicate(ctx):
if not isinstance(ctx.channel, discord.abc.GuildChannel):
return False
role = discord.utils.get(ctx.author.roles, name=name)
return role is None
return check(predicate)
@bot_has_permissions(manage_channels=True, manage_roles=True)
@has_permissions(manage_channels=True)
@BOT.command()
async def create(ctx, name: converter.clean_content(), side1: converter.clean_content(), side2: converter.clean_content()):
"""Sets up a debate"""
await ctx.send("This bot will be shutting down on April 30th. See why here: https://github.com/tweirtx/DebateBot")
name = name.strip().replace('\r', '').replace('\n', '')
side1 = side1.strip().replace('\r', '').replace('\n', '')
side2 = side2.strip().replace('\r', '').replace('\n', '')
if len(name) > 30:
await ctx.send("Debate name is too long!")
return
if len("{}-{}".format(name, side1)) > 32 or len("{}-{}".format(name, side2)) > 32:
await ctx.send("Please shorten the names, they are too long! Max combined length of debate name and sides "
"is 31 characters")
return
if side1 == side2:
await ctx.send("Side names cannot be the same!")
return
blacklist_role = discord.utils.get(ctx.guild.roles, name='debatebot-blacklist')
if blacklist_role is None:
await ctx.guild.create_role(name='debatebot-blacklist')
with db.Session() as session:
already_debate = session.query(Storage).filter_by(guild=ctx.guild.id).one_or_none()
if already_debate is not None:
await ctx.send("There is already a debate in this server! Multiple debates are not supported at this time.")
return
side1_role = await ctx.guild.create_role(name=side1)
side2_role = await ctx.guild.create_role(name=side2)
cat = await ctx.guild.create_category_channel(name)
await cat.set_permissions(target=ctx.guild.me, read_messages=True)
await cat.set_permissions(target=ctx.guild.default_role, send_messages=False)
main_channel = await ctx.guild.create_text_channel('{}-main'.format(name), category=cat)
await main_channel.set_permissions(target=side1_role, send_messages=True)
side1_channel = await ctx.guild.create_text_channel('{}-{}'.format(name, side1), category=cat)
await side1_channel.set_permissions(target=ctx.guild.default_role, read_messages=False)
await side1_channel.set_permissions(target=side1_role, read_messages=True, send_messages=True)
side2_channel = await ctx.guild.create_text_channel('{}-{}'.format(name, side2), category=cat)
await side2_channel.set_permissions(target=ctx.guild.default_role, read_messages=False)
await side2_channel.set_permissions(target=side2_role, read_messages=True, send_messages=True)
debate = Storage(guild=ctx.guild.id,
side1_role=side1_role.id,
side2_role=side2_role.id,
main_channel=main_channel.id,
side1_channel=side1_channel.id,
side2_channel=side2_channel.id,
side1_name=side1,
side2_name=side2,
admin=ctx.author.id)
session.add(debate)
await ctx.send("Debate {} with sides {} and {} created successfully".format(name, side1, side2))
await ctx.send("Side {} has the floor".format(side1))
@bot_has_permissions(manage_roles=True)
@bot_has_permissions(manage_roles=True)
@BOT.command()
async def floor(ctx, *, side):
"""Gives the floor to one side"""
await ctx.send("This bot will be shutting down on April 30th. See why here: https://github.com/tweirtx/DebateBot")
with db.Session() as session:
is_admin = session.query(Storage).filter_by(admin=ctx.author.id, guild=ctx.guild.id).one_or_none()
if is_admin is None:
await ctx.send("You are not the facilitator!")
return
side1 = session.query(Storage).filter_by(side1_name=side, guild=ctx.guild.id).one_or_none()
side2 = session.query(Storage).filter_by(side2_name=side, guild=ctx.guild.id).one_or_none()
if side1 is not None:
channel = ctx.guild.get_channel(channel_id=side1.main_channel)
role1 = discord.utils.get(ctx.guild.roles, id=side1.side1_role)
role2 = discord.utils.get(ctx.guild.roles, id=side1.side2_role)
await channel.set_permissions(target=role1, send_messages=True)
await channel.set_permissions(target=role2, send_messages=False)
await ctx.send("Side {} has the floor".format(side))
elif side2 is not None:
channel = ctx.guild.get_channel(channel_id=side2.main_channel)
role1 = discord.utils.get(ctx.guild.roles, id=side2.side1_role)
role2 = discord.utils.get(ctx.guild.roles, id=side2.side2_role)
await channel.set_permissions(target=role1, send_messages=False)
await channel.set_permissions(target=role2, send_messages=True)
await ctx.send("Side {} has the floor".format(side))
else:
await ctx.send("No side with that found! Please try again!")
locks = {}
@does_not_have_role(name='debatebot-blacklist')
@BOT.command()
async def join(ctx, *, side):
"""Lets a user join a side"""
lock = locks.get(ctx.guild.id)
if lock is None:
lock = locks[ctx.guild.id] = asyncio.Lock()
async with lock:
with db.Session() as session:
side1 = session.query(Storage).filter_by(side1_name=side, guild=ctx.guild.id).one_or_none()
side2 = session.query(Storage).filter_by(side2_name=side, guild=ctx.guild.id).one_or_none()
if side1 is not None:
role = side1.side1_role
existingroles = []
for i in ctx.author.roles:
existingroles.append(i.id)
if side1.side2_role in existingroles:
roletoremove = discord.utils.get(ctx.guild.roles, id=side1.side2_role)
await ctx.author.remove_roles(roletoremove)
elif side2 is not None:
role = side2.side2_role
existingroles = []
for i in ctx.author.roles:
existingroles.append(i.id)
if side2.side1_role in existingroles:
roletoremove = discord.utils.get(ctx.guild.roles, id=side2.side1_role)
await ctx.author.remove_roles(roletoremove)
else:
await ctx.send("Invalid side to join!")
return
actual_role = discord.utils.get(ctx.guild.roles, id=role)
await ctx.author.add_roles(actual_role)
await ctx.send("Successfully joined you to {} side!".format(side))
@bot_has_permissions(manage_roles=True)
@BOT.command()
async def leave(ctx):
"""Removes a user from a particular side"""
with db.Session() as session:
active_debate = session.query(Storage).filter_by(guild=ctx.guild.id).one_or_none()
role1 = discord.utils.get(ctx.guild.roles, id=active_debate.side1_role)
role2 = discord.utils.get(ctx.guild.roles, id=active_debate.side2_role)
sideroles = [role1, role2]
for role in ctx.author.roles:
if role in sideroles:
await ctx.author.remove_roles(role)
await ctx.send("{} has left".format(ctx.author.mention))
@bot_has_permissions(manage_channels=True, manage_roles=True)
@BOT.command()
async def end(ctx):
"""Ends a debate"""
await ctx.send("This bot will be shutting down on April 30th. See why here: https://github.com/tweirtx/DebateBot")
with db.Session() as session:
active_debate = session.query(Storage).filter_by(guild=ctx.guild.id).one_or_none()
if active_debate is None:
await ctx.send("No active debate found for this guild!")
return
elif active_debate.admin != ctx.author.id:
await ctx.send("You are not the admin, you don't have permission to do this!")
return
else:
main_channel = ctx.guild.get_channel(active_debate.main_channel)
cat_channel = main_channel.category
role1 = discord.utils.get(ctx.guild.roles, id=active_debate.side1_role)
role2 = discord.utils.get(ctx.guild.roles, id=active_debate.side2_role)
for i in [role1, role2]:
overwrite = cat_channel.overwrites_for(i)
main_overwrite = main_channel.overwrites_for(i)
if overwrite is not None:
overwrite.update(send_messages=False)
await cat_channel.set_permissions(target=i, overwrite=overwrite)
if main_overwrite is not None:
await main_channel.set_permissions(target=i, overwrite=None)
session.delete(active_debate)
await ctx.send("Debate ended successfully!")
@BOT.command()
async def github(ctx):
"""Links to source code"""
await ctx.send("Check out my bot code! You can see it here: https://github.com/tweirtx/DebateBot")
@BOT.event
async def on_ready():
"""Tells the host that it's ready"""
print("Ready!")
activity = discord.Activity(name="for April 30th.", type=discord.ActivityType.watching)
await BOT.change_presence(activity=activity, status=discord.Status.idle)
@BOT.event
async def on_command_error(ctx, exception):
await ctx.send(exception)
print(exception)
@BOT.event
async def on_message(message):
if message.author.bot:
return
await BOT.process_commands(message)
class Storage(db.DatabaseObject):
"""Stores everything"""
__tablename__ = 'debateStorage'
guild = db.Column(db.BigInteger, primary_key=True)
side1_role = db.Column(db.BigInteger)
side2_role = db.Column(db.BigInteger)
main_channel = db.Column(db.BigInteger)
side1_channel = db.Column(db.BigInteger)
side2_channel = db.Column(db.BigInteger)
side1_name = db.Column(db.String)
side2_name = db.Column(db.String)
admin = db.Column(db.BigInteger)
db.DatabaseObject.metadata.create_all()
BOT.run(Config.CONFIG['discord_token'])