forked from alphascriptyt/Discord_Rewrite_Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLIENTVERSIONdiscord_bot.py
336 lines (253 loc) · 12.8 KB
/
CLIENTVERSIONdiscord_bot.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# THIS IS FOR PEOPLE WHO USE CLIENT INSTEAD OF BOT
@client.event
async def on_ready():
client.reaction_roles = []
client.welcome_channels = {} # store like {guild_id : (channel_id, message)}
client.goodbye_channels = {}
client.sniped_messages = {}
client.ticket_configs = {}
client.warnings = {}
for guild in client.guilds:
async with aiofiles.open(f"{guild.id}.txt", mode="a") as temp:
pass
bot.warnings[guild.id] = {}
for file in ["reaction_roles.txt", "welcome_channels.txt", "goodbye_channels.txt", "ticket_configs.txt"]:
async with aiofiles.open(file, mode="a") as temp:
pass
async with aiofiles.open("reaction_roles.txt", mode="r") as file:
lines = await file.readlines()
for line in lines:
data = line.split(" ")
client.reaction_roles.append((int(data[0]), int(data[1]), data[2].strip("\n")))
async with aiofiles.open("welcome_channels.txt", mode="r") as file:
lines = await file.readlines()
for line in lines:
data = line.split(" ")
client.welcome_channels[int(data[0])] = (int(data[1]), " ".join(data[2:]).strip("\n"))
async with aiofiles.open("goodbye_channels.txt", mode="r") as file:
lines = await file.readlines()
for line in lines:
data = line.split(" ")
client.goodbye_channels[int(data[0])] = (int(data[1]), " ".join(data[2:]).strip("\n"))
async with aiofiles.open("ticket_configs.txt", mode="r") as file:
lines = await file.readlines()
for line in lines:
data = line.split(" ")
client.ticket_configs[int(data[0])] = [int(data[1]), int(data[2]), int(data[3])]
for guild in client.guilds:
async with aiofiles.open(f"{guild.id}.txt", mode="r") as file:
lines = await file.readlines()
for line in lines:
data = line.split(" ")
member_id = int(data[0])
admin_id = int(data[1])
reason = " ".join(data[2:]).strip("\n")
try:
client.warnings[guild.id][member_id][0] += 1
client.warnings[guild.id][member_id][1].append((admin_id, reason))
except KeyError:
client.warnings[guild.id][member_id] = [1, [(admin_id, reason)]]
print("Your bot is ready.")
@client.event
async def on_raw_reaction_add(payload):
for role_id, msg_id, emoji in bot.reaction_roles:
if msg_id == payload.message_id and emoji == str(payload.emoji.name.encode("utf-8")):
await payload.member.add_roles(client.get_guild(payload.guild_id).get_role(role_id))
return
if payload.member.id != client.user.id and str(payload.emoji) == u"\U0001F3AB":
msg_id, channel_id, category_id = client.ticket_configs[payload.guild_id]
if payload.message_id == msg_id:
guild = client.get_guild(payload.guild_id)
for category in guild.categories:
if category.id == category_id:
break
channel = guild.get_channel(channel_id)
ticket_num = 1 if len(category.channels) == 0 else int(category.channels[-1].name.split("-")[1]) + 1
ticket_channel = await category.create_text_channel(f"ticket {ticket_num}", topic=f"A channel for ticket number {ticket_num}.", permission_synced=True)
await ticket_channel.set_permissions(payload.member, read_messages=True, send_messages=True)
message = await channel.fetch_message(msg_id)
await message.remove_reaction(payload.emoji, payload.member)
await ticket_channel.send(f"{payload.member.mention} Thank you for creating a ticket! Use **'-close'** to close your ticket.")
try:
await client.wait_for("message", check=lambda m: m.channel == ticket_channel and m.author == payload.member and m.content == "-close", timeout=3600)
except asyncio.TimeoutError:
await ticket_channel.delete()
else:
await ticket_channel.delete()
@client.event
async def on_raw_reaction_remove(payload):
for role_id, msg_id, emoji in bot.reaction_roles:
if msg_id == payload.message_id and emoji == str(payload.emoji.name.encode("utf-8")):
guild = client.get_guild(payload.guild_id)
await guild.get_member(payload.user_id).remove_roles(guild.get_role(role_id))
return
@client.event
async def on_member_join(member):
for guild_id in client.welcome_channels:
if guild_id == member.guild.id:
channel_id, message = client.welcome_channels[guild_id]
await client.get_guild(guild_id).get_channel(channel_id).send(f"{message} {member.mention}")
return
@client.event
async def on_member_remove(member):
for guild_id in client.goodbye_channels:
if guild_id == member.guild.id:
channel_id, message = client.goodbye_channels[guild_id]
await client.get_guild(guild_id).get_channel(channel_id).send(f"{message} {member.mention}")
return
@client.event
async def on_message_delete(message):
client.sniped_messages[message.guild.id] = (message.content, message.author, message.channel.name, message.created_at)
#bot commands
@client.command()
@commands.has_permissions(administrator=True)
async def warnings(ctx, member: discord.Member=None):
if member is None:
return await ctx.send("The provided member could not be found or you forgot to provide one.")
embed = discord.Embed(title=f"Displaying Warnings for {member.name}", description="", colour=discord.Colour.red())
try:
i = 1
for admin_id, reason in client.warnings[ctx.guild.id][member.id][1]:
admin = ctx.guild.get_member(admin_id)
embed.description += f"**Warning {i}** given by: {admin.mention} for: *'{reason}'*.\n"
i += 1
await ctx.send(embed=embed)
except KeyError: # no warnings
await ctx.send("This user has no warnings.")
@client.command()
@commands.has_permissions(administrator=True)
async def warn(ctx, member: discord.Member=None, *, reason=None):
if member is None:
return await ctx.send("The provided member could not be found or you forgot to provide one.")
if reason is None:
return await ctx.send("Please provide a reason for warning this user.")
try:
first_warning = False
client.warnings[ctx.guild.id][member.id][0] += 1
client.warnings[ctx.guild.id][member.id][1].append((ctx.author.id, reason))
except KeyError:
first_warning = True
client.warnings[ctx.guild.id][member.id] = [1, [(ctx.author.id, reason)]]
count = client.warnings[ctx.guild.id][member.id][0]
async with aiofiles.open(f"{member.guild.id}.txt", mode="a") as file:
await file.write(f"{member.id} {ctx.author.id} {reason}\n")
@client.command()
async def configure_ticket(ctx, msg: discord.Message=None, category: discord.CategoryChannel=None):
if msg is None or category is None:
await ctx.channel.send("Failed to configure the ticket as an argument was not given or was invalid.")
return
client.ticket_configs[ctx.guild.id] = [msg.id, msg.channel.id, category.id] # this resets the configuration
async with aiofiles.open("ticket_configs.txt", mode="r") as file:
data = await file.readlines()
async with aiofiles.open("ticket_configs.txt", mode="w") as file:
await file.write(f"{ctx.guild.id} {msg.id} {msg.channel.id} {category.id}\n")
for line in data:
if int(line.split(" ")[0]) != ctx.guild.id:
await file.write(line)
await msg.add_reaction(u"\U0001F3AB")
await ctx.channel.send("Succesfully configured the ticket system.")
@client.command()
async def ticket_config(ctx):
try:
msg_id, channel_id, category_id = client.ticket_configs[ctx.guild.id]
except KeyError:
await ctx.channel.send("You have not configured the ticket system yet.")
else:
embed = discord.Embed(title="Ticket System Configurations", color=discord.Color.green())
embed.description = f"**Reaction Message ID** : {msg_id}\n"
embed.description += f"**Ticket Category ID** : {category_id}\n\n"
await ctx.channel.send(embed=embed)
@client.command()
async def snipe(ctx):
try:
contents, author, channel_name, time = bot.sniped_messages[ctx.guild.id]
except:
await ctx.channel.send("Couldn't find a message to snipe!")
return
embed = discord.Embed(description=contents, color=discord.Color.purple(), timestamp=time)
embed.set_author(name=f"{author.name}#{author.discriminator}", icon_url=author.avatar_url)
embed.set_footer(text=f"Deleted in : #{channel_name}")
await ctx.channel.send(embed=embed)
@client.command()
async def set_reaction(ctx, role: discord.Role=None, msg: discord.Message=None, emoji=None):
if role != None and msg != None and emoji != None:
await msg.add_reaction(emoji)
client.reaction_roles.append((role.id, msg.id, str(emoji.encode("utf-8"))))
async with aiofiles.open("reaction_roles.txt", mode="a") as file:
emoji_utf = emoji.encode("utf-8")
await file.write(f"{role.id} {msg.id} {emoji_utf}\n")
await ctx.channel.send("Reaction has been set.")
else:
await ctx.send("Invalid arguments.")
@client.command()
async def set_welcome_channel(ctx, new_channel: discord.TextChannel=None, *, message=None):
if new_channel != None and message != None:
for channel in ctx.guild.channels:
if channel == new_channel:
client.welcome_channels[ctx.guild.id] = (channel.id, message)
await ctx.channel.send(f"Welcome channel has been set to: {channel.name} with the message {message}")
await channel.send("This is the new welcome channel!")
async with aiofiles.open("welcome_channels.txt", mode="a") as file:
await file.write(f"{ctx.guild.id} {new_channel.id} {message}\n")
return
await ctx.channel.send("Couldn't find the given channel.")
else:
await ctx.channel.send("You didn't include the name of a welcome channel or a welcome message.")
@client.command()
async def set_goodbye_channel(ctx, new_channel: discord.TextChannel=None, *, message=None):
if new_channel != None and message != None:
for channel in ctx.guild.channels:
if channel == new_channel:
client.goodbye_channels[ctx.guild.id] = (channel.id, message)
await ctx.channel.send(f"Goodbye channel has been set to: {channel.name} with the message {message}")
await channel.send("This is the new goodbye channel!")
async with aiofiles.open("goodbye_channels.txt", mode="a") as file:
await file.write(f"{ctx.guild.id} {new_channel.id} {message}\n")
return
await ctx.channel.send("Couldn't find the given channel.")
else:
await ctx.channel.send("You didn't include the name of a goodbye channel or a goodbye message.")
@client.command()
async def hello(ctx):
channel = ctx.channel
await channel.send("Hi! " + str(ctx.author.mention))
@client.command()
async def ping(ctx, arg=None, option=1):
if arg == "pong":
await ctx.channel.send("You've already ponged yourself!")
else:
await ctx.channel.send(str(ctx.author.mention) + " Pong!")
if option == 1:
await ctx.channel.send("You chose option one!")
else:
await ctx.channel.send("You chose option " + str(option))
@client.command()
async def repeat(ctx, *, arg=None):
if arg == None:
await ctx.channel.send("You forgot to include an argument.")
else:
await ctx.channel.send(str(ctx.author.mention) + " " + str(arg))
@client.command()
async def dm(ctx, user_id=None, *, args=None):
if user_id != None and args != None:
try:
target = await bot.fetch_user(user_id)
await target.send(args)
await ctx.channel.send("'" + args + "' sent to: " + target.name)
except:
await ctx.channel.send("Couldn't dm the given user.")
else:
await ctx.channel.send("You didn't provide a user's id and/or a message.")
@client.command()
async def dm_all(ctx, *, args=None):
if args != None:
members = ctx.guild.members
for member in members:
try:
await member.send(args)
print("'" + args + "' sent to: " + member.name)
except:
print("Couldn't send '" + args + "' to: " + member.name)
else:
await ctx.channel.send("A message was not provided.")
client.run("token")