/
bot.py
249 lines (179 loc) · 7.7 KB
/
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
# ScottBot
# Author: Christopher James Walsh
# Created in August 2020 to spice up my friends' Discord server with funny games, a music player,
# and some moderation commands.
##### TO DO LIST #####
# - Implement a functional leveling system
# - Implement music capabilities
# - Implement a Steam gameprice checker with some sort of API
# - Add in more fun commands, possibly a currency system, etc.
# Import modules
import discord
import os
import random
import json
from aiohttp import request
from discord import Embed
from discord.ext import commands, tasks
from itertools import cycle
# Allows user to change prefix for the bot
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
# Create instance of the discord bot and assign to client variable
description = "Chatbot with some fun social, pre-programmed response, and moderation commands."
client = commands.Bot(command_prefix=get_prefix, description=description)
# Remove the default help command from this bot so that a custom help command can be created
client.remove_command('help')
# Create list of statuses for bot status changer to use
status = cycle(['Back to work!', 'No goofing off >:(',
'Noah smells good :)', 'Hi mom!', 'Conference room in 10', 'with the API', '.help'])
# GIFs for 'gamer night' event listener -- Currently not in use
#my_files = [
# discord.File('gifs/wheelchair1.gif'),
# discord.File('gifs/wheelchair2.gif'),
# discord.File('gifs/wheelchair3.gif')
#]
###### EVENTS ######
# Make sure the bot is online and set custom status
@client.event
async def on_ready():
print('**********')
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('**********')
change_status.start()
# Makes . default value for bot command prefix by manipulating json file
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '.'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
# Member join message
@client.event
async def on_member_join(member):
for channel in member.guild.channels:
if str(channel) == "general":
await channel.send(f"""The temp is here! Toby, get {member.mention} started on the new hire paperwork!""")
# Member leave message
@client.event
async def on_member_remove(member):
for channel in member.guild.channels:
if str(channel) == "general":
await channel.send(f"""Rest assured, {member.mention} won't be getting any severance pay. See ya!""")
# Bot sends quirky responses to certain keywords/phrases
@client.event
async def on_message(message):
if client.user.id != message.author.id:
if 'whomst' in message.content.lower():
await message.channel.send("Grab a beer, gamer night is @here")
await message.channel.send(file=discord.File('gifs/wheelchairgang.gif'))
elif 'blinkers' in message.content.lower():
await message.channel.send(file=discord.File('gifs/elon.gif'))
await message.channel.send(file=discord.File('gifs/blinkers.gif'))
elif 'Blinkers' in message.content.lower():
await message.channel.send(file=discord.File('gifs/elon.gif'))
await message.channel.send(file=discord.File('gifs/blinkers.gif'))
elif '@Blinkers' in message.content.lower():
await message.channel.send(file=discord.File('gifs/elon.gif'))
await message.channel.send(file=discord.File('gifs/blinkers.gif'))
elif 'bye' in message.content.lower():
await message.channel.send(file=discord.File('gifs/goodbye.gif'))
elif 'bot sucks' in message.content.lower():
await message.channel.send("no u")
await message.channel.send(file=discord.File('gifs/sad_mike.gif'))
elif 'no u' in message.content.lower():
await message.channel.send("no u")
await client.process_commands(message)
###### TASKS ######
@tasks.loop(seconds=10)
async def change_status():
await client.change_presence(activity=discord.Game(next(status)))
###### COMMANDS ######
# Load and Unload commands for loading and unloading cogs
@client.command(hidden=True)
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}')
@client.command(hidden=True)
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}')
# Custom help command
@client.command(hidden=True, brief="Displays the help dash")
async def help(ctx):
embed = Embed(
title="Help Commands",
description="A categorized list of Scottbot's commands! .help brings up this list!",
colour=ctx.author.colour
)
embed.set_thumbnail(
url="https://media2.giphy.com/media/55SfA4BxofRBe/giphy.gif?cid=ecf05e47n74f220gey16m63wccnqu5kcjw7eulr6z4n8c0l3&rid=giphy.gif")
embed.add_field(name="Moderator Commands:",
value=".ban - bans the mentioned user with reason argument\n.kick - kicks the mentioned user with reason argument",
inline=False)
embed.set_footer(text="Developed by CJW 2020")
embed.add_field(name="Gambling Commands:",
value=".coinflip - flip a coin\n.roll - roll a dice in 1dN format",
inline=False)
embed.add_field(name="Fun Commands:",
value=".conch - ask the Almighty Conch a question\n.insult - insult the mentioned user\n.compliment - compliment the mentioned user\n.animalfact - ask Scottbot for a fact about the target animal",
inline=False)
await ctx.send(embed=embed)
# Change command prefix
@client.command(brief="Change ScottBot's command prefix")
async def changeprefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix changed to: {prefix}')
# Clear command
@client.command(brief="Clears messages in current channel")
@commands.has_permissions(manage_messages=True)
@commands.cooldown(3, 10, commands.BucketType.user)
async def clear(ctx, amount: int):
amount += 1
if amount > 11:
await ctx.send("Nice try bucko, you can't delete over 10 messages at once!")
else:
await ctx.channel.purge(limit=amount)
# Check bot's ping (in milliseconds!)
@client.command(brief="Displays ScottBot's ping in milliseconds")
async def ping(ctx):
embed = Embed(
title=f"Pong!",
description=f"My ping is {round(client.latency * 1000)}ms!",
colour=ctx.author.colour)
await ctx.send(embed=embed)
###### ERRORS ######
# Bot command error message
@client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
embed = Embed(
title=f"Oops!",
description=f"Command not found! Type .help if you need the list of my commands!",
colour=ctx.author.colour)
await ctx.send(embed=embed)
# Clear command error message
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please specify an amount of messages to delete.')
# Import cogs
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}')
# Run bot using bot token from Discord
client.run(KEY)