-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquotes.py
127 lines (101 loc) · 5.44 KB
/
quotes.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
import json
import discord
from discord.ext import commands
from titlecase import titlecase
import parsers
from main import guild_id
from utils import create_embed, create_paginator
def setup(bot):
bot.add_cog(Quotes(bot))
class Quotes(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def get_quotes(self, ctx: discord.AutocompleteContext):
with open("data/quotes.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
quotes = data["quotes"]
results = []
for q in quotes:
quote = parsers.Quote.from_dict(q)
if ctx.value.lower() in quote.name.lower() or any(
ctx.value.lower() in alias.lower() for alias in quote.aliases):
results.append(titlecase(quote.name))
return results
@discord.slash_command(name="quote", description="Sends a quote.")
async def _quote(self, ctx, quote_name: discord.Option(str, "Quote Name", required=True, autocomplete=get_quotes),
member: discord.Option(discord.Member, "Member to Mention", required=False),
message_id: discord.Option(str, "Message ID to Reply To", required=False)):
with open("data/quotes.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
quotes = data["quotes"]
index = next((q for q in quotes if q["name"].lower() == quote_name.lower() or any(
alias.lower() == quote_name.lower() for alias in q["aliases"])), None)
quote = parsers.Quote.from_dict(index)
author = ctx.guild.get_member(quote.author).display_name
embed = create_embed(quote.name, description=quote.content, author=author, color=discord.Color.green(),
image=quote.image)
if member is not None:
await ctx.respond(member.mention, embed=embed)
elif message_id is not None:
message = await ctx.channel.fetch_message(message_id)
await message.reply(embed=embed)
await ctx.respond("Quote sent.", ephemeral=True)
else:
await ctx.respond(embed=embed)
@discord.slash_command(name="add_quote", description="Adds a quote.", guild_ids=[guild_id])
async def _add_quote(self, ctx, quote_name: discord.Option(str, "Quote Name", required=True),
quote_content: discord.Option(str, "Quote Content", required=True),
quote_image: discord.Option(discord.Attachment, "Quote Image", required=False),
quote_aliases: discord.Option(str, "Quote Aliases", required=False)):
with open("data/quotes.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
quotes = data["quotes"]
index = next((q for q in quotes if q["name"].lower() == quote_name.lower() or any(
alias.lower() == quote_name.lower() for alias in q["aliases"])), None)
if index is not None:
embed = create_embed("Quote already exists.")
return await ctx.respond(embed=embed)
aliases = quote_aliases.split(", ") if quote_aliases is not None else []
if quote_image is not None:
file = await quote_image.to_file()
embed = create_embed("Image quote created.", "**Do not delete this message.**")
message = await ctx.send(embed=embed, file=file)
quote_image_url = message.attachments[0].url
else:
quote_image_url = ""
quote = parsers.Quote(quote_name, ctx.author.id, quote_content, quote_image_url, aliases)
quotes.append(quote.to_dict())
with open("data/quotes.json", "w", encoding="utf-8") as fp:
json.dump(data, fp, indent=4)
embed = create_embed("Quote added.")
await ctx.respond(embed=embed, ephemeral=True)
@discord.slash_command(name="remove_quote", description="Removes a quote.", guild_ids=[guild_id])
async def _remove_quote(self, ctx,
quote_name: discord.Option(str, "Quote Name", required=True, autocomplete=get_quotes)):
with open("data/quotes.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
quotes = data["quotes"]
index = next((q for q in quotes if q["name"].lower() == quote_name.lower() or any(
alias.lower() == quote_name.lower() for alias in q["aliases"])), None)
if index is None:
embed = create_embed("Quote does not exist.")
return await ctx.respond(embed=embed)
quotes.remove(index)
with open("data/quotes.json", "w", encoding="utf-8") as fp:
json.dump(data, fp, indent=4)
embed = create_embed("Quote removed.")
await ctx.respond(embed=embed, ephemeral=True)
@discord.slash_command(name="all_quotes", description="Lists all quotes.", guild_ids=[guild_id])
async def _all_quotes(self, ctx):
with open("data/quotes.json", "r", encoding="utf-8") as fp:
data = json.load(fp)
quotes = data["quotes"]
embeds = []
for i in range(0, len(quotes)):
quote = parsers.Quote.from_dict(quotes[i])
author = ctx.guild.get_member(quote.author).display_name
embed = create_embed(quote.name, description=quote.content, author=author,
color=discord.Color.green(), image=quote.image)
embeds.append(embed)
paginator = create_paginator(embeds)
await paginator.respond(ctx.interaction, ephemeral=True)