/
bot.py
165 lines (138 loc) · 4.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
import random
import json
from os import path
from discord import Game
from discord.ext import commands as c
VERSION = "1.1.0"
# Grab the config variables
with open("config.json") as cfg:
config = json.load(cfg)
# Grab the databases
with open("db.json") as dtb:
db = json.load(dtb)
with open("nsfw.json") as nsfwf:
nsfw = json.load(nsfwf)
# Helper command to update the database
def update_db(database, filename):
with open(filename, "w") as dtb:
json.dump(database, dtb, indent=4)
description="""
Basic quote bot.
"""
bot = c.Bot(c.when_mentioned_or(config["prefix"]), description=description)
# Check for if the user running a command is the bot's owner.
def is_owner():
return c.check(lambda ctx: ctx.message.author.id == config["owner"])
# Change avatar and username
async def update_profile(name, picture):
if path.isfile(picture):
with open(picture, "rb") as avatar:
await bot.edit_profile(avatar=avatar.read())
print("Bot avatar set.")
await bot.edit_profile(username=name)
print("Bot name set.")
# Events
@bot.event
async def on_ready():
print("Attempting to set username and avatar.")
try:
await update_profile(config["bot_name"], config["avatar"])
except Exception as err:
print(f"Error setting name or avatar: {err}")
print("============READY")
print(f"QuoteBot v{VERSION}")
print(f"Logged in as: {bot.user.name} ({bot.user.id})")
print("============READY")
@bot.event
async def on_command_error(err, ctx):
channel = ctx.message.channel
if isinstance(err, c.CheckFailure):
await bot.send_message(channel, "You don't have permission to do that.")
elif isinstance(err, c.MissingRequiredArgument):
await bot.send_message(channel, "Missing argument(s).")
# Commands
@bot.command(name="quit")
@is_owner()
async def bot_quit():
"""Shut the bot down."""
await bot.say("Shutting down...")
await bot.logout()
@bot.command(name="status")
@is_owner()
async def bot_status(*, status: str = None):
"""Change the bot's 'playing' status.
Running this command without any arguments will turn the 'playing' status off'
"""
game = Game(name=status)
await bot.change_status(game=game)
@bot.command(name="info")
async def bot_info():
"""Display info about the bot."""
await bot.say(f"QuoteBot v{VERSION}")
@bot.group(aliases=["quotes"], pass_context=True)
async def quote(ctx):
"""Quotes!
Running the command without any arguments will display a random quote.
"""
if ctx.invoked_subcommand is None:
try:
quote_body = random.choice(db["quotes"])
except IndexError:
await bot.say("There are no quotes.")
return
await bot.say(quote_body)
@quote.command(name="add", aliases=["new", "create"])
async def quote_new(name, *, body: str):
"""Add a new quote."""
quote_body = f"<{name}> {body}"
if quote_body not in db["quotes"]:
db["quotes"].append(quote_body)
update_db(db, "db.json")
await bot.say("Quote added.")
else:
await bot.say("That quote already exists.")
@quote.command(name="remove", aliases=["del", "destroy"])
async def quote_remove(*, body: str):
"""Remove a quote.
Copy/Paste the entire line of a quote to remove it.
"""
if body in db["quotes"]:
db["quotes"].remove(body)
update_db(db, "db.json")
await bot.say("Quote removed.")
else:
await bot.say("Quote doesn't exist.")
@bot.group(name="nsfw", pass_context=True)
async def nsfw_c(ctx):
"""Not safe for work quotes!
Running the command without any arguments will display a random NSFW quote.
"""
if ctx.invoked_subcommand is None:
try:
nsfw_body = random.choice(nsfw["quotes"])
except IndexError:
await bot.say("There are no NSFW quotes.")
return
await bot.say(nsfw_body)
@nsfw_c.command(name="add", aliases=["new", "create"])
async def nsfw_new(name, *, body: str):
"""Add a new NSFW quote."""
nsfw_body = f"<{name}> {body}"
if nsfw_body not in nsfw["quotes"]:
nsfw["quotes"].append(nsfw_body)
update_db(nsfw, "nsfw.json")
await bot.say("NSFW quote added.")
else:
await bot.say("That NSFW quote already exists.")
@nsfw_c.command(name="remove", aliases=["del", "destroy"])
async def nsfw_remove(*, body: str):
"""Remove an NSFW quote.
Copy/Paste the entire line of an NSFW quote to remove it.
"""
if body in nsfw["quotes"]:
nsfw["quotes"].remove(body)
update_db(nsfw, "nsfw.json")
await bot.say("NSFW quote removed.")
else:
await bot.say("NSFW quote doesn't exist.")
bot.run(config["token"])