-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
120 lines (98 loc) · 3.45 KB
/
bot.py
File metadata and controls
120 lines (98 loc) · 3.45 KB
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
# -*- coding: utf-8 -*-
import discord
import asyncio
import json
from discord.ext.commands import Bot
"""
Settings for prefix, token and extensions are found in
the config.json file for easy manipulation.
"""
try:
with open("config.json") as data_file:
data = json.loads(data_file.read())
data_file.close()
except FileNotFoundError:
print("Configuration not found.")
exit
TOKEN = data["token"]
BOT_PREFIX = data["prefix"]
STARTUP_EXT = data["extensions"]
LOOP = asyncio.get_event_loop()
client = Bot(command_prefix=BOT_PREFIX,
description="A generic bot to test functionality with the" +
"discord.py API")
@client.event
async def on_message(message):
await client.process_commands(message)
@client.event
async def on_ready():
await client.change_presence(game=discord.Game(name='in space'))
print('Successfully logged in as ' + client.user.name)
"""
This simple version changes the bot's default prefix on the config file.
Todo - implement server-based prefix selection.
"""
@client.command(name="prefix",
description="Changes the bot's prefix.")
async def prefix(prefix: str=""):
if prefix != "":
with open("config.json") as data_file:
data = json.loads(data_file.read())
data_file.close()
data["prefix"] = prefix
with open("config.json", "w") as data_file:
json.dump(data, data_file)
client.command_prefix = prefix
else:
await client.say("No prefix was provided.")
"""
Reloads extensions through the STARTUP_EXT array. Extensions are .py
files with commands to be attached to the bot. This method of
implementation allows for the user to keep the bot up and reload
its commands as they are altered by simply using the reload command.
"""
@client.command(name="reload",
hidden=True,
pass_context=True)
async def reload(context):
app_info = await client.application_info()
owner = app_info.owner
if context.message.author.id == owner.id:
for exten in STARTUP_EXT:
try:
client.unload_extension(exten)
client.load_extension(exten)
print('Successfully reloaded extension ' + exten)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to operate extension {}\n{}'.format(exten, exc))
if client.is_voice_connected(context.message.server):
await client.voice_client_in(context.message.server).disconnect()
print("Disconnected from voice channel.")
else:
print("User " + context.message.author.name + " attempted to reload" +
"extensions")
"""
Loads extensions through the STARTUP_EXT array. Extensions are .py
files with commands to be attached to the bot.
"""
if __name__ == "__main__":
for extension in STARTUP_EXT:
try:
client.load_extension(extension)
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension {}\n{}'.format(extension, exc))
"""
Runs the bot. It can be shut down through a KeyboardInterrupt in the
console it is running.
"""
try:
LOOP.run_until_complete(client.start(TOKEN))
except KeyboardInterrupt:
LOOP.run_until_complete(client.logout())
print("Logging out through keyboard interruption.")
# cancel all tasks lingering
finally:
print("Bot stopped.")
LOOP.close()