diff --git a/cogs/events.py b/cogs/events.py index 44526b3..1b47d3e 100644 --- a/cogs/events.py +++ b/cogs/events.py @@ -30,7 +30,7 @@ async def on_command_error(self, ctx: commands.Context, error: commands.CommandE return await ctx.send(f"Oops, you didn't type the command correctly, {ctx.author.mention}.\nUse `{ctx.prefix}help {ctx.command.name}` for more information.") if isinstance(error, commands.NotOwner): - return + return await ctx.send(f"This command is available only to the bot owners, {ctx.author.mention}.") exception = traceback.format_exception(type(error), error, error.__traceback__) diff --git a/cogs/impersonation.py b/cogs/impersonation.py index 3a239c2..29de1c9 100644 --- a/cogs/impersonation.py +++ b/cogs/impersonation.py @@ -1,5 +1,4 @@ -from discord import Message, User -from discord import Embed, Colour +from discord import Message, User, Embed, Colour, NotFound from discord.ext import commands from motor.motor_asyncio import AsyncIOMotorCollection @@ -79,6 +78,9 @@ async def impersonate(self, ctx: commands.Context, victim: Optional[User] = None webhook = await self.webhooks.get(ctx.channel, session=session) + if not webhook: + webhook = await self.webhooks.create(ctx.channel) + if not content: await ctx.message.remove_reaction("⏲️", ctx.me) @@ -106,6 +108,9 @@ async def fakekick(self, ctx: commands.Context, victim: Optional[User] = None): session = aiohttp.ClientSession() webhook = await self.webhooks.get(ctx.channel, session=session) + if not webhook: + webhook = await self.webhooks.create(ctx.channel) + message = self.bot.config["Commands"]["Fakekick"]["content"] message = message.format(user=victim) @@ -126,6 +131,9 @@ async def gold(self, ctx: commands.Context): session = aiohttp.ClientSession() webhook = await self.webhooks.get(ctx.channel, session=session) + if not webhook: + webhook = await self.webhooks.create(ctx.channel) + message = self.bot.config["Commands"]["Gold"]["content"] message = message.format(guild=ctx.guild) @@ -137,9 +145,32 @@ async def gold(self, ctx: commands.Context): await webhook.send(embed=embed, avatar_url=ctx.author.display_avatar.url, username=ctx.author.name) await session.close() - @commands.command() - async def delwebhook(self, ctx: commands.Context): - await WebhookManager.remove(self.webhooks, ctx.channel) + @commands.command(aliases=["delwebhook"]) + @commands.is_owner() + async def delhook(self, ctx: commands.Context): + """ + Deletes a webhook made by the bot inside the channel where the command is being executed. + Only available to the bot owners. + + **Arguments:** + None. + """ + + session = aiohttp.ClientSession() + webhook = await self.webhooks.get(ctx.channel, session=session) + + if not webhook: + return await ctx.message.reply("There is no webhook in this channel.") + + try: + await webhook.delete() + except NotFound: + pass + + await self.webhooks.remove(ctx.channel) + + await ctx.message.reply("Webhook deleted successfully.") + await session.close() def setup(bot: commands.Bot): bot.add_cog(Impersonation(bot=bot, diff --git a/modules/webhooks.py b/modules/webhooks.py index 1c3de96..9ec847c 100644 --- a/modules/webhooks.py +++ b/modules/webhooks.py @@ -1,4 +1,4 @@ -from __future__ import annotations +from typing import Optional from aiohttp import ClientSession from discord import Webhook, TextChannel @@ -9,11 +9,11 @@ def __init__(self, database: AsyncIOMotorDatabase): self.collection = database.webhooks # methods for interacting with a specific webhook in a collection - async def get(self, channel: TextChannel, session: ClientSession) -> Webhook: + async def get(self, channel: TextChannel, session: ClientSession) -> Optional[Webhook]: webhook = await self.collection.find_one({"channel": {"id": str(channel.id)}}) if webhook is None: - return await self.create(channel) + return return Webhook.partial(id=int(webhook.get("id")), token=webhook.get("token"), session=session) @@ -29,4 +29,4 @@ async def create(self, channel: TextChannel) -> Webhook: return webhook async def remove(self, channel: TextChannel): - return await self.collection.delete_one({"channel": {"id": str(channel.id)}}) \ No newline at end of file + await self.collection.delete_one({"channel": {"id": str(channel.id)}}) \ No newline at end of file