async def on_raw_reaction_add(self, reaction: RawReactionActionEvent): if str(reaction.emoji.id) in plopkoek_emote: channel = await self.bot.fetch_channel(reaction.channel_id) message: Message = await channel.fetch_message(reaction.message_id) receiver = message.author donator = reaction.member bot_id = get_value("main", "bot_id") if receiver.id == bot_id or message.webhook_id == get_value( "quotebot", "webhook_id" ): quote_content = message.content if receiver.display_name == bot_display_name and " -" in quote_content: quote_content = "-".join(quote_content.split(" -")[:-1]) found = False for quotee, quotes in get_value("quotebot", "quotes").items(): for quote in quotes: if quote["quote"] == quote_content: receiver = await self.get_user(quotee) or receiver found = True if not found: receiver = await self.get_user(str(bot_id)) await self.add_plopkoek(receiver, donator, message)
async def send_random_quote(self, ctx: Context, user: Optional[Union[Member, User, str]]): """ Send a "random" quote. This is triggered by a `!quotebot random [username]` command. """ user_id = user user_name = user if isinstance(user, Member) or isinstance(user, User): user_id = str(user.id) user_name = user.display_name quotes = get_value("quotebot", "quotes") try: if user_id: if user_id in quotes: quote = random.choice( [q["quote"] for q in quotes[user_id]]) await post_quote(ctx.channel, quote, user) else: msg = "BEEP BOOP, 404 {} not found!".format(user_name) await post_message(ctx.channel, msg) else: q = get_random_quote() user = await self.get_user(q["quotee"]) if user is None: user = q["quotee"] await post_quote(ctx.channel, q["quote"], user) except IndexError: post_message(ctx.channel, "No quotes..")
def start_cogs(cogs: List[str]): bot = Bot(command_prefix=prefixes) for cog in cogs: bot.load_extension(f"cogs.{cog}") bot.run(get_value("main", "discord_token"))
async def find_quote(self, ctx: Context, user: Union[Member, User, str], *keywords: str): """ Find quotes containing a given pattern. This is triggered by a `!quotebot find <username> <keyword>` command. Pass * as the username to search through all quotes. """ search_all_users = user == "*" user_id = user user_name = user if isinstance(user, Member) or isinstance(user, User): user_id = str(user.id) user_name = user.display_name all_quotes = get_value("quotebot", "quotes") messages: List[str] = [] if not search_all_users and user_id not in all_quotes: messages.append(f"Could not find {user_name}") else: sentence = " ".join(keywords) found: Dict[str, List[str]] = defaultdict(list) for quotee, quotes in all_quotes.items(): if not search_all_users and quotee != user_id: continue for quote in quotes: if sentence.lower() in quote["quote"].lower(): found[quotee].append(quote["quote"]) if len(found) == 0: if search_all_users: messages.append( f"Could not find any occurence of '{sentence}'") else: messages.append( f"Could not find {user_name} saying '{sentence}'") else: found_length = sum(len(v) for v in found.values()) if found_length == 1: user = next(iter(found.keys())) await post_quote( ctx.channel, next(iter(found.values()))[0], await self.get_user(user) or user, ) return elif found_length > 25: messages.append( f"Found more than 25 quotes matching '{sentence}'. Skipping output." ) else: messages.append( f"Found these quotes containing '{sentence}':\n") for quotee, quotes in found.items(): user_name = await self.get_user_name(quotee) messages.append(f"{user_name}: {' | '.join(quotes)}\n") for message in messages: await post_message(ctx.channel, message)
def get_random_quote(): """ Return a random quote from the quote list. """ quotelist = [] for quotee, quotes in get_value("quotebot", "quotes").items(): for quote in quotes: quotelist.append({"quote": quote["quote"], "quotee": quotee}) return random.choice(quotelist)
async def list_quotees(self, ctx: Context): """ List all users with a quote in the database. This is triggered by a `!quotebot quotees` command. """ quotes = get_value("quotebot", "quotes") names = [await self.get_user_name(user) for user in quotes.keys()] msg = " | ".join(sorted(names)) await post_message(ctx.channel, msg)
async def show_stats(self, ctx: Context): """ Show the total quote count and a top 5 of users with most quotes """ quotes = get_value("quotebot", "quotes") counter = Counter({quotee: len(quotes[quotee]) for quotee in quotes}) await post_message(ctx.channel, f"Total quote count: {sum(counter.values())}") msg = "Quote top 5:\n" for quotee, num in counter.most_common(5): msg += f" {num}: {await self.get_user_name(quotee)}\n" await post_message(ctx.channel, msg)
async def add_quote(self, ctx: Context, user: Union[Member, User, str], *quote: str): """ Add a quote to the database. This is triggered by a `!quotebot add <username> <quote>` command. """ user_id = user if isinstance(user, User) or isinstance(user, Member): user_id = str(user.id) quotes = get_value("quotebot", "quotes") quote_dict = { "quote": " ".join(quote), "added_by": ctx.author.id, "added_on": str(datetime.now()), } quotes.setdefault(user_id, []).append(quote_dict) set_value("quotebot", "quotes", quotes) await post_message(ctx.channel, content="Quote added!")
async def list_quotes(self, ctx: Context, user: Union[Member, User, str]): """ List all quotes for a given user. This is triggered by a `!quotebot list <username>` command. """ user_id = user user_name = user if isinstance(user, Member) or isinstance(user, User): user_id = str(user.id) user_name = user.display_name quotes = get_value("quotebot", "quotes") if user_id in quotes: msg = f"{user_name}'s quotes are: " msg += " | ".join([q["quote"] for q in quotes[user_id]]) else: msg = f"Could not find {user_name} :(\nUse `!quotebot quotees` to list all users with a quote)" await post_message(ctx.channel, msg)
from discord import Webhook, RequestsWebhookAdapter from discord.channel import TextChannel from discord.ext.commands.cog import Cog from discord.member import Member from discord.message import Message from discord.user import User from discord.ext.commands.bot import Bot from discord.ext.commands.context import Context from api.cog import PlopCog from api.decorators import command from api.utils import get_value, set_value quote_url = "https://cdn1.iconfinder.com/data/icons/anchor/128/quote.png" webhook_id = get_value("quotebot", "webhook_id") webhook_token = get_value("quotebot", "webhook_token") general_channel_id = get_value("main", "general_channel_id") def get_random_quote(): """ Return a random quote from the quote list. """ quotelist = [] for quotee, quotes in get_value("quotebot", "quotes").items(): for quote in quotes: quotelist.append({"quote": quote["quote"], "quotee": quotee}) return random.choice(quotelist)
from api.decorators import command from api.utils import get_value from .db import ( delete_plopkoek, get_alltime_ranking, get_donations_left, get_month_ranking, get_total_income, has_donated_plopkoek, init_db, get_income, insert_plopkoek, ) general_channel_id = get_value("main", "general_channel_id") plopkoek_emote = get_value("plopkoek", "emote") bot_display_name = get_value("plopkoek", "display_name") def can_donate(donator, receiver): if donator == receiver: return False return get_donations_left(donator) > 0 def filter_ascii_only(data): return [ [d[0], d[1], "".join([c for c in d[2] if c in string.printable])] for d in data ]
import sys from typing import List from discord.ext.commands import Bot from api.utils import get_value prefixes = ("!quotebot ", "!plopkoekbot ", "!qb ", "!pk ") if get_value("main", "test_env"): prefixes = tuple(f"~{prefix[1:]}" for prefix in prefixes) def start_cogs(cogs: List[str]): bot = Bot(command_prefix=prefixes) for cog in cogs: bot.load_extension(f"cogs.{cog}") bot.run(get_value("main", "discord_token")) if __name__ == "__main__": start_cogs(cogs=sys.argv[1:])