Skip to content
This repository has been archived by the owner on Aug 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #33 from Deivedux/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Deivedux authored Apr 28, 2019
2 parents cecc734 + 138f3eb commit e28b2f8
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 74 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[![Support Server](https://discordapp.com/api/guilds/418455732741079040/widget.png?style=shield)](https://discord.gg/sbySHxA)
[![Quote Bot](https://discordbots.org/api/widget/status/447176783704489985.svg)](https://discordbots.org/bot/447176783704489985)
[![Quote Bot](https://discordbots.org/api/widget/upvotes/447176783704489985.svg)](https://discordbots.org/bot/447176783704489985/vote)
[![Code Quality](https://api.codacy.com/project/badge/Grade/81a0a0e33ddd4a32882fe57ebb5d60a1)](https://app.codacy.com/app/aki-jp/Quote?utm_source=github.com&utm_medium=referral&utm_content=Deivedux/Quote&utm_campaign=Badge_Grade_Dashboard)
[![Upvote](https://aki-toga.tk/css/support.png)](https://discordbots.org/bot/447176783704489985/vote)

# Quote and reply to messages on Discord!
Quote is a Discord bot that allows users to easily quote messages, a feature that any serious messaging platform should have but is missing from Discord.
Expand Down
78 changes: 47 additions & 31 deletions cogs/Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,8 @@ class Main(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.Cog.listener()
async def on_ready(self):
c.execute("DELETE FROM ServerConfig WHERE Guild NOT IN (" + ', '.join([str(guild.id) for guild in self.bot.guilds]) + ")")
conn.commit()

@commands.Cog.listener()
async def on_guild_remove(self, guild):
c.execute("DELETE FROM ServerConfig WHERE Guild = " + str(guild.id))
conn.commit()

try:
del prefixes[guild.id]
except KeyError:
Expand All @@ -83,6 +75,22 @@ async def on_guild_remove(self, guild):
except ValueError:
pass

@commands.Cog.listener()
async def on_guild_join(self, guild):
try:
c.execute("INSERT INTO ServerConfig (Guild) VALUES (" + str(guild.id) + ")")
conn.commit()
except sqlite3.IntegrityError:
pass

db_response = c.execute("SELECT * FROM ServerConfig WHERE Guild = " + str(guild.id)).fetchone()
if db_response[1] != None:
prefixes[int(db_response[0])] = str(db_response[1])
if db_response[2] != None:
del_commands.append(int(db_response[0]))
if db_response[3] != None:
on_reaction.append(int(db_response[0]))

@commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
if str(payload.emoji) == '💬' and payload.user_id not in blacklist_ids and not self.bot.get_guild(payload.guild_id).get_member(payload.user_id).bot and payload.guild_id in on_reaction:
Expand All @@ -104,29 +112,39 @@ async def on_raw_reaction_add(self, payload):
await channel.send(embed = quote_embed(channel, message, user))

@commands.command(aliases = ['q'])
@commands.cooldown(1, 3, type = commands.BucketType.channel)
async def quote(self, ctx, msg_id: int = None, *, reply = None):
if not msg_id:
return await ctx.send(content = error_string + ' **Please specify a message ID to quote.**')
@commands.cooldown(2, 3, type = commands.BucketType.channel)
async def quote(self, ctx, msg_arg = None, *, reply = None):
if not msg_arg:
return await ctx.send(content = error_string + ' **Please provide a valid message argument.**')

if ctx.guild and ctx.guild.id in del_commands and ctx.guild.me.permissions_in(ctx.channel).manage_messages:
await ctx.message.delete()

message = None
try:
message = await ctx.channel.fetch_message(msg_id)
except:
for channel in ctx.guild.text_channels:
perms = ctx.guild.me.permissions_in(channel)
if channel == ctx.channel or not perms.read_messages or not perms.read_message_history:
continue

try:
message = await channel.fetch_message(msg_id)
except:
continue
else:
break
msg_arg = int(msg_arg)
except ValueError:
perms = ctx.guild.me.permissions_in(ctx.channel)
if perms.read_messages and perms.read_message_history:
async for msg in ctx.channel.history(limit = 100, before = ctx.message):
if msg_arg.lower() in msg.content.lower():
message = msg
break
else:
try:
message = await ctx.channel.fetch_message(msg_arg)
except:
for channel in ctx.guild.text_channels:
perms = ctx.guild.me.permissions_in(channel)
if channel == ctx.channel or not perms.read_messages or not perms.read_message_history:
continue

try:
message = await channel.fetch_message(msg_arg)
except:
continue
else:
break

if message:
if not message.content and message.embeds and message.author.bot:
Expand Down Expand Up @@ -240,12 +258,10 @@ async def duplicate(self, ctx, msgs: int, channel: discord.TextChannel):
webhook = await ctx.channel.create_webhook(name = 'Message Duplicator')

for msg in messages:
try:
async with aiohttp.ClientSession() as session:
webhook_channel = discord.Webhook.from_url(webhook.url, adapter = discord.AsyncWebhookAdapter(session))
await webhook_channel.send(username = msg.author.display_name, avatar_url = msg.author.avatar_url, content = msg.content, embeds = msg.embeds, wait = True)
except:
pass
async with aiohttp.ClientSession() as session:
webhook_channel = discord.Webhook.from_url(webhook.url, adapter = discord.AsyncWebhookAdapter(session))
await webhook_channel.send(username = msg.author.display_name, avatar_url = msg.author.avatar_url, content = msg.content, embeds = msg.embeds, wait = True)
await asyncio.sleep(0.5)

await webhook.delete()

Expand Down
53 changes: 21 additions & 32 deletions cogs/PersonalQuotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def list_embed(list_personals, author, page_number):
embed = discord.Embed(description = '\n'.join(['• `' + i[1] + '`' for i in list_personals]), color = author.color)
else:
embed = discord.Embed(description = '\n'.join(['• `' + i[1] + '`' for i in list_personals]))
embed.set_author(name = 'My Quotes', icon_url = author.avatar_url)
embed.set_author(name = 'Personal Quotes', icon_url = author.avatar_url)
embed.set_footer(text = 'Page: ' + str(page_number))
return embed

Expand All @@ -47,52 +47,35 @@ def __init__(self, bot):

@commands.command(aliases = ['padd'])
async def personaladd(self, ctx, trigger, *, response = None):
db_response = c.execute("SELECT Trigger FROM PersonalQuotes WHERE User = " + str(ctx.author.id) + " AND Trigger = '" + trigger.replace('\'', '\'\'') + "'").fetchone()
if db_response:
return await ctx.send(content = error_string + ' **You already have a quote with that trigger.**')

if not response and not ctx.message.attachments:
return await ctx.send(content = error_string + ' **You must include at least a response or an attachment in your message.**')

if response and ctx.message.attachments:
if len(ctx.message.attachments) == 1 and ctx.message.attachments[0].url.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.gifv', '.webp', '.bmp')):
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Response, Attachments) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + response.replace('\'', '\'\'') + "', '" + ctx.message.attachments[0].url.replace('\'', '\'\'') + "')")
conn.commit()
else:
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Response, Attachments) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + response.replace('\'', '\'\'') + "', '" + ' | '.join(['[' + attachment.filename.replace('\'', '\'\'') + '](' + attachment.url.replace('\'', '\'\'') + ')' for attachment in ctx.message.attachments]) + "')")
conn.commit()
elif not response and ctx.message.attachments:
if len(ctx.message.attachments) == 1 and ctx.message.attachments[0].url.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.gifv', '.webp', '.bmp')):
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Attachments) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + ctx.message.attachments[0].url.replace('\'', '\'\'') + "')")
conn.commit()
else:
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Attachments) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + ' | '.join(['[' + attachment.filename.replace('\'', '\'\'') + '](' + attachment.url.replace('\'', '\'\'') + ')' for attachment in ctx.message.attachments]) + "')")
conn.commit()
else:
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Response) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + response.replace('\'', '\'\'') + "')")
conn.commit()
try:
c.execute("INSERT INTO PersonalQuotes (User, Trigger" + (", Response" if response else "") + (", Attachments" if ctx.message.attachments else "") + ") VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "'" + (", '" + response.replace('\'', '\'\'') + "'" if response else "") + (", '" + " | ".join([attachment.url for attachment in ctx.message.attachments]).replace('\'', '\'\'') + "'" if ctx.message.attachments else "") + ")")
conn.commit()
except sqlite3.IntegrityError:
return await ctx.send(content = error_string + ' **You already have a quote with that trigger.**')

await ctx.send(content = success_string + ' **Quote added.**')

@commands.command(aliases = ['qr'])
async def qradd(self, ctx, trigger, *, response = None):
db_response = c.execute("SELECT * FROM PersonalQuotes WHERE User = " + str(ctx.author.id) + " AND Trigger = '" + trigger.replace('\'', '\'\'') + "'").fetchone()
if db_response:
return await ctx.send(content = error_string + ' **You already have a quote with that trigger.**')

if not response and not ctx.message.attachments:
return await ctx.send(content = error_string + ' **QR code must not be empty.**')

qr_url = 'https://chart.googleapis.com/chart?' + urllib.parse.urlencode({'cht': 'qr', 'chs': '200x200', 'chld': 'L|1', 'chl': response})
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Attachments) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + qr_url.replace('\'', '\'\'') + "')")
conn.commit()
try:
c.execute("INSERT INTO PersonalQuotes (User, Trigger, Attachments) VALUES (" + str(ctx.author.id) + ", '" + trigger.replace('\'', '\'\'') + "', '" + qr_url.replace('\'', '\'\'') + "')")
conn.commit()
except:
return await ctx.send(content = error_string + ' **You already have a quote with that trigger.**')

await ctx.send(content = success_string + ' **Quote added.**')

@commands.command(aliases = ['premove', 'prem'])
async def personalremove(self, ctx, *, trigger):
user_quotes = c.execute("SELECT * FROM PersonalQuotes WHERE User = " + str(ctx.author.id)).fetchall()
if trigger in [i[1] for i in user_quotes]:
user_quotes = c.execute("SELECT * FROM PersonalQuotes WHERE User = " + str(ctx.author.id) + " AND Trigger = '" + trigger.replace('\'', '\'\'') + "'").fetchone()
if trigger:
c.execute("DELETE FROM PersonalQuotes WHERE User = " + str(ctx.author.id) + " AND Trigger = '" + trigger.replace('\'', '\'\'') + "'")
conn.commit()
await ctx.send(content = success_string + ' **Quote deleted.**')
Expand All @@ -103,7 +86,7 @@ async def personalremove(self, ctx, *, trigger):
async def personal(self, ctx, *, trigger):
user_quote = c.execute("SELECT * FROM PersonalQuotes WHERE User = " + str(ctx.author.id) + " AND Trigger = '" + trigger.replace('\'', '\'\'') + "'").fetchone()
if not user_quote:
await ctx.send(content = error_string + ' **No quote with that trigger exist.**')
await ctx.send(content = error_string + ' **Quote with that trigger does not exist.**')
else:
if ctx.guild and ctx.guild.id in del_commands and ctx.guild.me.permissions_in(ctx.channel).manage_messages:
await ctx.message.delete()
Expand All @@ -114,10 +97,16 @@ async def personal(self, ctx, *, trigger):
async def personallist(self, ctx, page_number: int = 1):
user_quotes = c.execute("SELECT * FROM PersonalQuotes WHERE User = " + str(ctx.author.id) + " LIMIT 10 OFFSET " + str(10 * (page_number - 1))).fetchall()
if len(user_quotes) == 0:
await ctx.send(content = error_string + ' **No personal quotes on page ' + str(page_number) + '.**')
await ctx.send(content = error_string + ' **No personal quotes on page `' + str(page_number) + '`**')
else:
await ctx.send(embed = list_embed(user_quotes, ctx.author, page_number))

@commands.command(aliases = ['pclear'])
async def personalclear(self, ctx):
c.execute("DELETE FROM PersonalQuotes WHERE User = " + str(ctx.author.id))
conn.commit()
await ctx.send(content = success_string + ' **Cleared all your personal quotes.**')


def setup(bot):
bot.add_cog(PersonalQuotes(bot))
15 changes: 10 additions & 5 deletions commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
},
"quote": {
"title": ["quote", "q"],
"description": "Quote a message using a message ID, and optionally leave your own reply to a quoted message.",
"examples": ["quote 426100904874213387", "q 426100904874213387 This is my reply"]
"description": "Quote a message using a message ID or by saying a part of the message (case-insensitive), and optionally leave your own reply to a quoted message.",
"examples": ["quote 426100904874213387", "q 426100904874213387 This is my reply", "quote message this is my reply", "quote \"this message\" my reply"]
},
"personaladd": {
"title": ["personaladd", "padd"],
"description": "Add your personal quote, with a trigger and a response, which only you can trigger in any server. A maximum of 10 personal quotes are allowed.",
"description": "Add your personal quote, with a trigger and a response, which only you can trigger in any server. A response can be any number of attachments and/or a follow-up message after the trigger.",
"examples": ["padd trigger response", "padd \"my trigger\" my response"]
},
"qradd": {
Expand All @@ -36,8 +36,13 @@
},
"personallist": {
"title": ["personallist", "plist"],
"description": "List your personal quotes.",
"examples": ["plist"]
"description": "List your personal quotes. 10 quotes per page.",
"examples": ["plist", "plist 2"]
},
"personalclear": {
"title": ["personalclear", "pclear"],
"description": "Deletes all your personal quotes.",
"examples": ["pclear"]
},
"prefix": {
"title": ["prefix"],
Expand Down
2 changes: 2 additions & 0 deletions docs/Guides/Credentials Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

> `token`, enter your bot token which can be found at [Discord Developers Page](https://discordapp.com/developers/applications/me). *See GIF below.*
> `bot_id`, enter your bot ID. *See how to get IDs in the GIF below.*
> `default_prefix`, the default prefix of your Quote bot. You can change it to whatever you like.
> `owner_ids`, these are the IDs of users who can use the owner only commands, including yourself. *See how to get IDs in the GIF below.*
Expand Down
6 changes: 3 additions & 3 deletions docs/Guides/Windows Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
> Downloading:
![](https://aki-toga.tk/quote/Download.gif)
![](https://i.imgur.com/ClPfBB3.gif)

> Editing `config.json`:
![](https://aki-toga.tk/quote/Edit%20JSON.gif)
![](https://i.imgur.com/SAkdLhp.gif)

> Launching:
![](https://aki-toga.tk/quote/Launching.gif)
![](https://i.imgur.com/8DaO9ra.gif)

---

Expand Down
2 changes: 1 addition & 1 deletion quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Create necessary database tables, if they don't exist already, on it's own behalf.
c.execute("CREATE TABLE IF NOT EXISTS ServerConfig (Guild INTEGER unique, Prefix TEXT, DelCommands TEXT, OnReaction TEXT, PinChannel INTEGER)")
c.execute("CREATE TABLE IF NOT EXISTS Blacklist (Id INTEGER unique, Reason TEXT)")
c.execute("CREATE TABLE IF NOT EXISTS PersonalQuotes (User INTEGER, Trigger TEXT, Response TEXT, Attachments TEXT)")
c.execute("CREATE TABLE IF NOT EXISTS PersonalQuotes (User INTEGER, Trigger TEXT, Response TEXT, Attachments TEXT, PRIMARY KEY (User, Trigger))")
c.execute("CREATE TABLE IF NOT EXISTS Donators (UserId INTEGER unique, UserTag TEXT)")

from cogs.Main import prefixes
Expand Down

0 comments on commit e28b2f8

Please sign in to comment.