Skip to content

Commit

Permalink
Update bot.py (#162)
Browse files Browse the repository at this point in the history
* Update bot.py

* Fix spelling lol

* [ci] Autoformat with black

---------

Co-authored-by: github-actions <github-actions@example.com>
  • Loading branch information
mishl-dev and github-actions authored Jul 4, 2024
1 parent 0e3524f commit 971527f
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions cogs/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,29 @@ async def invite(self, ctx):
normal_inv = discord.utils.oauth_url(
self.bot.user.id, permissions=discord.Permissions(permissions=8), scopes=("bot",)
)
minimial_invite = discord.utils.oauth_url(
minimal_invite = discord.utils.oauth_url(
self.bot.user.id, permissions=discord.Permissions(permissions=70634561), scopes=("bot",)
)

normal_inv_slash = discord.utils.oauth_url(
self.bot.user.id,
permissions=discord.Permissions(permissions=8),
)
minimial_invite_slash = discord.utils.oauth_url(
minimal_invite_slash = discord.utils.oauth_url(
self.bot.user.id,
permissions=discord.Permissions(permissions=70634561),
)

embed = discord.Embed(title="Invite link:", color=random.randint(0, 16777215))
embed.add_field(
name=f"{self.bot.user.name} invite:",
value=f"[{self.bot.user.name} invite url]({normal_inv}) \nNon Markdowned invite : {normal_inv}",
value=f"[{self.bot.user.name} invite url]({normal_inv}) \nNon Markdown invite: {normal_inv}",
)
embed.add_field(name="Minimial permisions", value=f"{ minimial_invite}")
embed.add_field(name="Minimal permissions", value=f"{minimal_invite}")

embed.set_thumbnail(url=self.bot.user.display_avatar.url)
embed.set_footer(
text="not all features may work if you invite with minimal perms, if you invite with 0 make sure these permissions are in a Bots/Bot role."
text="Not all features may work if you invite with minimal permissions. If you invite with 0 permissions, make sure these permissions are in a Bots/Bot role."
)

view = discord.ui.View()
Expand All @@ -120,24 +120,24 @@ async def invite(self, ctx):
)
view.add_item(
discord.ui.Button(
label=f"{self.bot.user.name}'s Minimial Permisions Invite",
url=minimial_invite,
label=f"{self.bot.user.name}'s Minimal Permissions Invite",
url=minimal_invite,
style=discord.ButtonStyle.link,
)
)

view.add_item(
discord.ui.Button(
label=f"{self.bot.user.name}'s Normal Invite(Slash)",
label=f"{self.bot.user.name}'s Normal Invite (Slash)",
url=normal_inv_slash,
style=discord.ButtonStyle.link,
row=2,
)
)
view.add_item(
discord.ui.Button(
label=f"{self.bot.user.name}'s Minimial Permisions(Slash)",
url=minimial_invite_slash,
label=f"{self.bot.user.name}'s Minimal Permissions (Slash)",
url=minimal_invite_slash,
style=discord.ButtonStyle.link,
row=2,
)
Expand Down Expand Up @@ -241,50 +241,77 @@ async def command_autocomplete(
brief="finds out where the location of the command on my github repo(so people can learn from my commands)"
)
async def source(self, ctx, *, command=None):
await self.get_source(ctx, command)
embed = discord.Embed(
title="Github link",
description=f"{self.github_url}",
color=15428885,
timestamp=ctx.message.created_at,
)
embed.set_footer(
text="This Bot's License is MIT, you must credit if you use my code, but please just make your own, if you don't know something works ask me, or try to learn how mine works."
)

if command is None:
await ctx.send("Here's the github link:", embed=embed)
return

try:
src, item_name = await self.fetch_source(ctx, command)
if src is None:
await ctx.send(embed=embed)
return

github_url, filename, lines, firstline = self.process_source(src)

file_url = f"{github_url}/blob/{self.branch}/{filename}"
line_url = f"{file_url}#L{firstline}-L{firstline + len(lines) - 1}"
embed.title = f"Source for {item_name}:"
embed.description = f"[**Click Here**]({line_url})"
await ctx.send(embed=embed)

except Exception as e:
await ctx.send(f"An error occurred while fetching the source: {str(e)}")

@app_commands.command(name="source", description="Find the source code for a command, cog, or utility function")
@app_commands.autocomplete(item=command_autocomplete)
async def source_slash(self, interaction: discord.Interaction, item: typing.Optional[str] = None):
await self.get_source(interaction, item)

async def get_source(self, ctx, item=None):
embed = discord.Embed(
title="Github link",
description=f"{self.github_url}",
color=15428885,
timestamp=ctx.created_at if isinstance(ctx, discord.Interaction) else ctx.message.created_at,
timestamp=interaction.created_at,
)
embed.set_footer(
text="This Bot's License is MIT, you must credit if you use my code, but please just make your own, if you don't know something works ask me, or try to learn how mine works."
)

if item is None:
return await self.send_response(ctx, "Here's the github link:", embed=embed)
await interaction.response.send_message("Here's the github link:", embed=embed)
return

try:
src, item_name = await self.fetch_source(ctx, item)
src, item_name = await self.fetch_source(interaction, item)
if src is None:
return await self.send_response(ctx, embed=embed)
await interaction.response.send_message(embed=embed)
return

github_url, filename, lines, firstline = self.process_source(src)

file_url = f"{github_url}/blob/{self.branch}/{filename}"
line_url = f"{file_url}#L{firstline}-L{firstline + len(lines) - 1}"
embed.title = f"Source for {item_name}:"
embed.description = f"[**Click Here**]({line_url})"
await self.send_response(ctx, embed=embed)
await interaction.response.send_message(embed=embed)

except Exception as e:
await self.send_response(ctx, f"An error occurred while fetching the source: {str(e)}")
await interaction.response.send_message(f"An error occurred while fetching the source: {str(e)}")

async def fetch_source(self, ctx, item):
if isinstance(ctx, discord.Interaction):
item_type, item_name = item.split(":", 1)
if item_type == "command":
command_wanted = self.bot.get_command(item_name) or self.bot.tree.get_command(item_name)
if not command_wanted:
await self.send_response(ctx, f"Couldn't find command {item_name}. Here's source anyway:")
return None, None
return (
command_wanted.callback
Expand All @@ -294,27 +321,22 @@ async def fetch_source(self, ctx, item):
elif item_type == "cog":
cog = self.bot.get_cog(item_name)
if not cog:
await self.send_response(ctx, f"Couldn't find cog {item_name}. Here's source anyway:")
return None, None
return type(cog), item_name
elif item_type == "util":
try:
utils = importlib.import_module("utils")
src = getattr(utils, item_name)
if not src:
await self.send_response(ctx, f"Couldn't find utility {item_name}. Here's source anyway:")
return None, None
return src, item_name
except (ImportError, AttributeError):
await self.send_response(ctx, f"Couldn't find utility {item_name}. Here's source anyway:")
return None, None
else:
await self.send_response(ctx, f"Unknown item type {item_type}. Here's source anyway:")
return None, None
else:
command_wanted = self.bot.get_command(item)
if not command_wanted:
await self.send_response(ctx, f"Couldn't find {item}. Here's source anyway:")
return None, None
return command_wanted.callback, item

Expand Down

0 comments on commit 971527f

Please sign in to comment.