Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions techsupport_bot/botlogging/delayed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import os
from typing import Any

from botlogging import logger

Expand All @@ -17,13 +18,13 @@ class DelayedLogger(logger.BotLogger):
queue_size (int): the max number of queue events
"""

def __init__(self, *args, **kwargs):
def __init__(self, *args: tuple, **kwargs: dict[str, Any]) -> None:
self.wait_time = kwargs.pop("wait_time", 1)
self.queue_size = kwargs.pop("queue_size", 1000)
self.__send_queue = None
super().__init__(*args, **kwargs)

async def send_log(self, *args, **kwargs) -> None:
async def send_log(self, *args: tuple, **kwargs: dict[str, Any]) -> None:
"""Adds a log to the queue
Does nothing different than the Logger send_log function()
Will disregard debug logs if debug is off
Expand Down
17 changes: 6 additions & 11 deletions techsupport_bot/commands/dumpdbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ async def setup(bot) -> None:
bot.add_extension_config("dumpdbg", config)


class DumpdbgEmbed(discord.Embed):
"""Class to set up the dumpdbg embed."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.color = discord.Color.green()


class Dumpdbg(cogs.BaseCog):
"""Class for the dump debugger on the discord bot."""

Expand Down Expand Up @@ -148,17 +140,19 @@ async def debug_dump(self, ctx: commands.Context) -> None:
# Formatting for several files because it looks prettier
if len(result_urls) == 1:
await ctx.send(
embed=DumpdbgEmbed(
embed=auxiliary.generate_basic_embed(
title="Dump succesfully debugged! \nResult links:",
description="\n".join(result_urls),
color=discord.Color.green(),
),
content=auxiliary.construct_mention_string([ctx.author]),
)
else:
await ctx.send(
embed=DumpdbgEmbed(
embed=auxiliary.generate_basic_embed(
title="Dumps succesfully debugged! \nResult links:",
description="\n".join(result_urls),
color=discord.Color.green(),
)
)

Expand Down Expand Up @@ -188,9 +182,10 @@ async def get_files(self, ctx: commands.Context) -> list[str]:
# Disregards any empty dumps
if attachment.size == 0:
await ctx.send(
embed=DumpdbgEmbed(
embed=auxiliary.generate_basic_embed(
title="Invalid dump detected (Size 0)",
description=f"Skipping dump number {dump_no}...",
color=discord.Color.green(),
)
)
continue
Expand Down
22 changes: 7 additions & 15 deletions techsupport_bot/commands/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ async def setup(bot):
bot.add_extension_config("gate", config)


class WelcomeEmbed(discord.Embed):
"""Class to welcome a user to the discord, then delete the message."""

def __init__(self, *args, **kwargs):
welcome_message = kwargs.pop("welcome_message")
delete_wait = kwargs.pop("delete_wait")
super().__init__(*args, **kwargs)
self.title = "Server Gate"
self.description = welcome_message
self.set_footer(text=f"This message will be deleted in {delete_wait} seconds")
self.color = discord.Color.green()


class ServerGate(cogs.MatchCog):
"""Class to get the server gate from config."""

Expand Down Expand Up @@ -117,8 +104,13 @@ async def response(self, config, ctx: commands.Context, content, _):
welcome_message = config.extensions.gate.welcome_message.value
delete_wait = config.extensions.gate.delete_wait.value

embed = WelcomeEmbed(
welcome_message=welcome_message, delete_wait=delete_wait
embed = auxiliary.generate_basic_embed(
title="Server Gate",
description=welcome_message,
color=discord.Color.green(),
)
embed.set_footer(
text=f"This message will be deleted in {delete_wait} seconds"
)
await ctx.send(
embed=embed,
Expand Down
27 changes: 8 additions & 19 deletions techsupport_bot/commands/google.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Module for the google extension for the discord bot."""

import discord
import ui
from core import auxiliary, cogs, extensionconfig
from discord.ext import commands
Expand Down Expand Up @@ -31,24 +30,14 @@ async def setup(bot):
bot.add_extension_config("google", config)


class GoogleEmbed(discord.Embed):
"""Class for the google embed for discord."""

ICON_URL = (
"https://cdn.icon-icons.com/icons2/673/PNG/512/Google_icon-icons.com_60497.png"
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.color = discord.Color.blurple()
self.set_thumbnail(url=self.ICON_URL)


class Googler(cogs.BaseCog):
"""Class for the google extension for the discord bot."""

GOOGLE_URL = "https://www.googleapis.com/customsearch/v1"
YOUTUBE_URL = "https://www.googleapis.com/youtube/v3/search?part=id&maxResults=10"
ICON_URL = (
"https://cdn.icon-icons.com/icons2/673/PNG/512/Google_icon-icons.com_60497.png"
)

async def get_items(self, url, data):
"""Method to get an item from google's api."""
Expand Down Expand Up @@ -101,11 +90,11 @@ async def search(self, ctx, *, query: str):
for index, item in enumerate(items):
link = item.get("link")
snippet = item.get("snippet", "<Details Unknown>").replace("\n", "")
embed = (
GoogleEmbed(title=f"Results for {query}")
if field_counter == 1
else embed
)
if field_counter == 1:
embed = auxiliary.generate_basic_embed(
title=f"Results for {query}", url=self.ICON_URL
)

embed.add_field(name=link, value=snippet, inline=False)
if (
field_counter == config.extensions.google.max_responses.value
Expand Down
3 changes: 1 addition & 2 deletions techsupport_bot/commands/hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ class HangmanGame:
]
FINAL_STEP = len(HANG_PICS) - 1

def __init__(self, **kwargs):
word = kwargs.pop("word")
def __init__(self, word: str) -> None:
if not word or "_" in word or not word.isalpha():
raise ValueError("valid word must be provided")
self.word = word
Expand Down
39 changes: 22 additions & 17 deletions techsupport_bot/commands/kanye.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ async def setup(bot):
bot.add_extension_config("kanye", config)


class KanyeEmbed(discord.Embed):
"""Class for the Kanye embed for discord."""
class KanyeQuotes(cogs.LoopCog):
"""Class to get the Kanye quotes from the api."""

API_URL = "https://api.kanye.rest"
KANYE_PICS = [
"https://i.imgur.com/ITmTXGz.jpg",
"https://i.imgur.com/o8BkPrL.jpg",
Expand All @@ -48,19 +49,23 @@ class KanyeEmbed(discord.Embed):
"https://i.imgur.com/g1o2Gro.jpg",
]

def __init__(self, *args, **kwargs):
quote = kwargs.pop("quote")
super().__init__(*args, **kwargs)
self.set_thumbnail(url=random.choice(self.KANYE_PICS))
self.color = discord.Color.dark_gold()
self.title = f'"{quote}"'
self.description = "Kanye West"


class KanyeQuotes(cogs.LoopCog):
"""Class to get the Kanye quotes from the api."""

API_URL = "https://api.kanye.rest"
def generate_themed_embed(self, quote: str) -> discord.Embed:
"""Generates a themed embed for the kayne plugin
Includes adding the quote, changing the color, and adding an icon

Args:
quote (str): The quote to put in the embed

Returns:
discord.Embed: The formatted embed, ready to be sent
"""
embed = auxiliary.generate_basic_embed(
title=f'"{quote}"',
description="Kanye West",
color=discord.Color.dark_gold(),
url=random.choice(self.KANYE_PICS),
)
return embed

async def get_quote(self):
"""Method to get the quote from the api."""
Expand All @@ -70,7 +75,7 @@ async def get_quote(self):
async def execute(self, config, guild):
"""Method to execute and give the quote to discord."""
quote = await self.get_quote()
embed = KanyeEmbed(quote=quote)
embed = self.generate_themed_embed(quote=quote)

channel = guild.get_channel(int(config.extensions.kanye.channel.value))
if not channel:
Expand All @@ -95,6 +100,6 @@ async def wait(self, config, _):
async def kanye(self, ctx):
"""Method to call the command on discord."""
quote = await self.get_quote()
embed = KanyeEmbed(quote=quote)
embed = self.generate_themed_embed(quote=quote)

await ctx.send(embed=embed)
54 changes: 22 additions & 32 deletions techsupport_bot/commands/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,50 +36,39 @@ async def convert(self, ctx, argument: int):
return channel


class ListenEmbed(discord.Embed):
"""Base embed for listen events."""
class Listener(cogs.BaseCog):
"""Cog object for listening to channels."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.color = discord.Color.blurple()
self.timestamp = datetime.datetime.utcnow()
MAX_DESTINATIONS = 10
CACHE_TIME = 60

def format_message_in_embed(self, message: discord.Message) -> discord.Embed:
"""Formats a listened message into a pretty embed

class MessageEmbed(ListenEmbed):
"""Embed for message events."""
Args:
message (discord.Message): The raw message to format

def __init__(self, *args, **kwargs):
message = kwargs.pop("message")
super().__init__(*args, **kwargs)
self.set_author(
Returns:
discord.Embed: The stylized embed ready to be sent
"""
embed = auxiliary.generate_basic_embed(description=message.clean_content)

embed.timestamp = datetime.datetime.utcnow()
embed.set_author(
name=message.author.name, icon_url=message.author.display_avatar.url
)

self.description = message.clean_content
if message.embeds:
self.description = f"{self.description} (includes embed)"
embed.description = f"{embed.description} (includes embed)"

if message.attachments:
self.add_field(
embed.add_field(
name="Attachments", value=" ".join(a.url for a in message.attachments)
)

self.set_footer(text=f"#{message.channel.name} - {message.guild}")
embed.set_footer(text=f"#{message.channel.name} - {message.guild}")


class InfoEmbed(discord.Embed):
"""Embed for providing info about listener jobs."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.color = discord.Color.green()


class Listener(cogs.BaseCog):
"""Cog object for listening to channels."""

MAX_DESTINATIONS = 10
CACHE_TIME = 60
return embed

async def preconfig(self):
"""Preconfigures the listener cog."""
Expand Down Expand Up @@ -356,8 +345,9 @@ async def jobs(self, ctx):
)
return

embed = InfoEmbed(
embed = auxiliary.generate_basic_embed(
title="Listener Registrations",
color=discord.Color.green(),
)
for source_obj in source_objects:
src_ch = source_obj.get("source")
Expand Down Expand Up @@ -390,7 +380,7 @@ async def on_message(self, message: discord.Message):
if not destinations:
return
for dst in destinations:
embed = MessageEmbed(message=message)
embed = self.format_message_in_embed(message=message)
await dst.send(embed=embed)

@commands.Cog.listener()
Expand Down
18 changes: 5 additions & 13 deletions techsupport_bot/commands/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ async def setup(bot):
await bot.add_cog(StrawPoller(bot=bot))


class PollEmbed(discord.Embed):
"""Class for the poll embed for discord."""

def __init__(self, *args, **kwargs):
thumbnail_url = kwargs.pop("thumbnail_url")
super().__init__(*args, **kwargs)
self.set_thumbnail(url=thumbnail_url)
self.color = discord.Color.gold()


class PollGenerator(cogs.BaseCog):
"""Class to make the poll generator for the extension."""

Expand Down Expand Up @@ -157,10 +147,11 @@ async def generate(self, ctx):
)
display_timeout_units = "seconds" if request_body.timeout <= 60 else "minutes"

embed = PollEmbed(
embed = auxiliary.generate_basic_embed(
title=request_body.question,
description=f"Poll timeout: {display_timeout} {display_timeout_units}",
thumbnail_url=request_body.image_url,
color=discord.Color.gold(),
url=request_body.image_url,
)

for index, option in enumerate(request_body.options):
Expand Down Expand Up @@ -201,9 +192,10 @@ async def generate(self, ctx):
)
return

embed = PollEmbed(
embed = auxiliary.generate_basic_embed(
title=f"Poll results for `{request_body.question}`",
description=f"Votes: {total}",
color=discord.Color.gold(),
thumbnail_url=request_body.image_url,
)

Expand Down
Loading