Skip to content

Commit

Permalink
restrict the command for bot owners only and make it delete the webho…
Browse files Browse the repository at this point in the history
…ok from discord as well
  • Loading branch information
bemxio committed Dec 4, 2024
1 parent e390f78 commit c0b648d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cogs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
41 changes: 36 additions & 5 deletions cogs/impersonation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions modules/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations
from typing import Optional

from aiohttp import ClientSession
from discord import Webhook, TextChannel
Expand All @@ -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)

Expand All @@ -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)}})
await self.collection.delete_one({"channel": {"id": str(channel.id)}})

0 comments on commit c0b648d

Please sign in to comment.