This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
219 lines (176 loc) · 6.89 KB
/
main.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
#Needed imports
import discord, sys, json
from asyncio import sleep
#import the command auto complete and test
import command
#Build-a-Wumpus/profiles library
import baw
#Import the db handler
import db_handler
#Set the client instance
client = discord.Client()
#Load the token
with open("config.json", "r") as conf_file:
config = json.load(conf_file)
token = config["token"]
#Runs when they bot starts
@client.event
async def on_ready():
print("The Wumpus is ready!")
#Automatically update the status to show usercount
client.loop.create_task(update_status())
print("Started custom status!")
#get the prefixes from the db
command.update_prefixes()
#Update the sql db with all the prefixes from the json
client.loop.create_task(update_db())
print("Started DB updater!")
#When it joins a server it add the prefix to the list
@client.event
async def on_server_join(server):
#Add the prefix to the list
command.prefixes[server.id] = command.prefixes["default"]
#Go thorugh all the members and give them accounts if they don't exist
for user in [x for x in server.members if not x.bot]:
db_handler.new_user(user.id)
#Whenever it sees a new member join a server
@client.event
async def on_member_join(member):
if not member in list(client.get_all_members()):
db_handler.new_user(member.id)
#Dealign with commands
@client.event
async def on_message(message):
#Ingore any messages from bots
if (message.author.bot):
return
#Test and autocomplete the command
cmd = command.test_command(message.content, message.server)
#It doesn't start with the prefix ignore it
if cmd["status"] == 4:
return
#If the status was 0 (not a valid command or it doesn't start with the prefix)
if cmd["status"] == 0:
desc = "That wasn't a valid command! Use {}help for a list of commands!".format(cmd["prefix"])
embed = discord.Embed(title="Invalid Command", description=desc, color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#If there are multiple matches suggest them to the user
if cmd["status"] == 2:
desc = '\n {}'.format( cmd["prefix"] ).join( [""] + cmd["matches"] )
embed = discord.Embed(title="Did you mean", description=desc, color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#If something broken
if cmd["status"] == -1:
desc = "Well... this is awkward. Something broke and will be fixed soon. Try again in a few minutes."
embed = discord.Embed(title="Wumpus tripped over a wire", description=desc, color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#If all the checks passed then get the command and arguments
args = cmd["args"]
prefix = cmd["prefix"]
cmd = cmd["matches"][0]
#Stop the bot
if cmd == "stop":
#Write the prefixes so the prefix is saved
command.write_prefixes()
sys.exit()
#Show the help/usage message
if cmd == "help":
desc = """
{p}botstats - Shows statistics about the bot
{p}help - Shows this help message
{p}blacksmiths - Lists the development team
{p}ping - Pong!
{p}code - Link to the github (Yay! Open Source!)
{p}prefix [New Prefix] - Set the prefix for the server
{p}profile - Show you profile
{p}baw - Starts build a wumpus in your DM's
Other features
-Command auto-completion for when you're too lazy to type out the whole command
-Build-a-Wumpus
""".format(p=prefix)
#Embeds look nice
embed = discord.Embed(title="Wumpus Bot Help", description=desc, color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#Pong!
if cmd == "ping":
await client.send_message(message.channel, "Pong! {}".format(' '.join(args)))
return
#Displays the developers names
if cmd == "blacksmiths":
desc = "This is a bot that was written for Discord Hack Week.\nThe development team is <@388777079455612929>, <@140611354317815808> and, of course, Wumpus"
embed = discord.Embed(title="Blacksmiths", description=desc, color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#Shows the bot statistics
if cmd == "botstats":
#Get the statistics
users = len([x for x in list(dict.fromkeys(list(client.get_all_members()))) if str(x.status) != "offline"])
servers = len(client.servers)
#Send them in an embed so it looks cool
embed = discord.Embed(title="Bot Statistics", description="Bot Version - Alpha\nGaming with {} discordians in {} servers".format(users,servers), color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#Show the users profile
if cmd == "profile":
#Get the users id
userid = message.author.id
#If there is mention use that instead
if len(message.mentions) > 0:
userid = message.mentions[0].id
#Send the profile picture
image = baw.get_profile(str(userid))
await client.send_file(message.channel, image)
return
#Build a wumpus!
if cmd == "baw":
if message.server != None:
#Check if it is in a DM or not
embed = discord.Embed(title="Need to be in a DM!", description="You need to be in a DM to use this command!", color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#Start Build-a-Wumpus
await baw.build_a_wumpus(discord, client, message.channel, message.author.id)
return
#Share a link to the github
if cmd == "code":
await client.send_message(message.channel, "https://github.com/Fraserbc/Wumpus_Bot")
return
#Set the prefix command
if cmd == "prefix":
#Check they have the correct permissions
if message.server == None or not message.author.server_permissions.administrator:
embed = discord.Embed(title="Permission Denied", description="You need to have administrator permissions in this server to change the prefix!", color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#They have permission so set the prefix so check if they supplied an argument
if len(args) == 0:
embed = discord.Embed(title="No Arguments Supplied", description="You need to supply an argument!\nCheck {}help for the syntax".format(prefix), color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#Set the prefix
command.prefixes[message.server.id] = args[0]
embed = discord.Embed(title="Prefix Changed", description="You have successfully changed the prefix to {}".format(args[0]), color=0x972ed9)
await client.send_message(message.channel, embed=embed)
return
#Update status every 10 seconds
async def update_status():
while True:
#How many users are online (removes duplicates)
users = len([x for x in list(dict.fromkeys(list(client.get_all_members()))) if str(x.status) != "offline"])
#Change the status
await client.change_presence(game=discord.Game(name='Games with {} discordians'.format(users)))
#Sleep so I don't DOS the API
await sleep(10)
#Update the db every 5 mins
async def update_db():
while True:
#Update the prefixes
command.write_prefixes()
#Wait for 5 mins
await sleep(60*5)
#Start the bot
client.run(token)