|
| 1 | +import emoji |
| 2 | +import discord |
| 3 | +from discord import app_commands |
| 4 | +from discord.ext import commands |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from src.utils import Embeds, Error |
| 8 | +from src.config import Config |
| 9 | + |
| 10 | + |
| 11 | +class Misc(commands.Cog): |
| 12 | + """Miscellaneous commands""" |
| 13 | + |
| 14 | + client: commands.Bot |
| 15 | + |
| 16 | + |
| 17 | + def __init__(self, client): |
| 18 | + self.client = client |
| 19 | + |
| 20 | + |
| 21 | + @commands.Cog.listener() |
| 22 | + async def on_ready(self): |
| 23 | + print('Miscellaneous Commands UP') |
| 24 | + |
| 25 | + |
| 26 | + @commands.hybrid_command( |
| 27 | + name = 'ping', |
| 28 | + description = 'Print out the current ping', |
| 29 | + with_app_command = True, |
| 30 | + required = True |
| 31 | + ) |
| 32 | + @commands.cooldown(1, 15) |
| 33 | + async def ping(self, ctx: commands.Context): |
| 34 | + """ |
| 35 | + **Calculates the bot's latency** |
| 36 | +
|
| 37 | + > **Example** |
| 38 | + ``` |
| 39 | + /ping |
| 40 | + ``` |
| 41 | + """ |
| 42 | + replication_rate = 60 |
| 43 | + latency = sum([self.client.latency for _ in range(replication_rate)])/replication_rate |
| 44 | + await ctx.reply(f'{ctx.author.mention}', embed = Embeds( |
| 45 | + title = 'Pong! :ping_pong:', |
| 46 | + description = f'{round(latency*1000, 2)}ms', |
| 47 | + color = discord.Color.purple() |
| 48 | + )) |
| 49 | + |
| 50 | + |
| 51 | + @commands.hybrid_command( |
| 52 | + name = 'links', |
| 53 | + description = 'Returns the project links', |
| 54 | + with_app_command = True, |
| 55 | + ) |
| 56 | + @commands.cooldown(1, 10, commands.BucketType.channel) |
| 57 | + async def links(self, ctx: commands.Context): |
| 58 | + """ |
| 59 | + **Returns the project links** |
| 60 | +
|
| 61 | + > **Example** |
| 62 | + ``` |
| 63 | + /links |
| 64 | + ``` |
| 65 | + """ |
| 66 | + await ctx.reply(ctx.author.mention, embed = Embeds( |
| 67 | + title = 'Project Links', |
| 68 | + description = f'[**Github Organization**](https://github.com/python-thread)\n[**Documentation**](https://thread.ngjx.org)' |
| 69 | + )) |
| 70 | + |
| 71 | + |
| 72 | + @commands.hybrid_command( |
| 73 | + name = 'getprefix', |
| 74 | + description = 'Returns the current in-use command prefix', |
| 75 | + with_app_command = True, |
| 76 | + required = True, |
| 77 | + aliases = ['prefix'] |
| 78 | + ) |
| 79 | + @commands.cooldown(1, 15, commands.BucketType.channel) |
| 80 | + async def getprefix(self, ctx: commands.Context): |
| 81 | + """ |
| 82 | + **Returns the current in-use command prefix** |
| 83 | +
|
| 84 | + > **Aliases** |
| 85 | + ``` |
| 86 | + prefix, getprefix |
| 87 | + ``` |
| 88 | +
|
| 89 | + > **Example** |
| 90 | + ``` |
| 91 | + /prefix |
| 92 | + /getprefix |
| 93 | + ``` |
| 94 | + """ |
| 95 | + await ctx.reply(ctx.author.mention, embed = Embeds( |
| 96 | + title = 'Prefix', |
| 97 | + description = str(Config.COMMAND_PREFIX) |
| 98 | + )) |
| 99 | + |
| 100 | + |
| 101 | + @commands.hybrid_command( |
| 102 | + name = 'react', |
| 103 | + description = 'Reacts to the message with specified reaction!', |
| 104 | + with_app_command = True, |
| 105 | + aliases = ['addreaction'], |
| 106 | + required = True |
| 107 | + ) |
| 108 | + @app_commands.describe( |
| 109 | + reaction = 'What reaction you would like to add', |
| 110 | + messageid = 'Which message you would like to add to' |
| 111 | + ) |
| 112 | + @commands.cooldown(1, 5, commands.BucketType.user) |
| 113 | + async def addreaction(self, ctx: commands.Context, reaction: str, messageid: Optional[str] = None): |
| 114 | + """ |
| 115 | + **Reacts to the message with specified reaction!** |
| 116 | +
|
| 117 | + > **Arguments** |
| 118 | + ``` |
| 119 | + reaction | string | required |
| 120 | + messageid | string | default = Latest message in channel |
| 121 | + ``` |
| 122 | +
|
| 123 | + > **Example** |
| 124 | + ``` |
| 125 | + /react [str] (str) |
| 126 | + /react :ping_pong: 12345 |
| 127 | + /react :ping_pong: https://.../12345 |
| 128 | + ``` |
| 129 | + """ |
| 130 | + if not ctx.guild or not ctx.message.guild: return |
| 131 | + |
| 132 | + if not emoji.is_emoji(reaction): |
| 133 | + for emojiinfo in ctx.message.guild.emojis: |
| 134 | + if emojiinfo.name == reaction: |
| 135 | + reaction = emojiinfo.animated and f'<a:{emojiinfo.name}:{emojiinfo.id}>' or f'<:{emojiinfo.name}:{emojiinfo.id}>' |
| 136 | + break |
| 137 | + |
| 138 | + if messageid: |
| 139 | + id = messageid.startswith('https:') and messageid.split('/')[len(messageid.split('/')) - 1] or messageid |
| 140 | + elif isinstance(ctx.message.channel, discord.TextChannel): |
| 141 | + id = str(ctx.message.channel.last_message_id) |
| 142 | + else: |
| 143 | + raise Error('Unsupported channel type') |
| 144 | + |
| 145 | + message = await ctx.message.channel.fetch_message(int(id)) |
| 146 | + await message.add_reaction(reaction) |
| 147 | + |
| 148 | + embed = Embeds(title = 'Success') |
| 149 | + embed.set_author(name = ctx.author.name, icon_url = ctx.author.display_avatar) |
| 150 | + embed.add_field(name = "Emoji", value = f'`{reaction}`', inline = False) |
| 151 | + embed.add_field( |
| 152 | + name = "Message ID", |
| 153 | + value = f'https://discord.com/channels/{ctx.guild.id}/{ctx.message.channel.id}/{id}', |
| 154 | + inline = False |
| 155 | + ) |
| 156 | + |
| 157 | + await ctx.reply(ctx.author.mention, embed = embed, ephemeral = True) |
| 158 | + |
| 159 | + |
| 160 | +async def setup(client: commands.Bot): |
| 161 | + await client.add_cog(Misc(client)) |
0 commit comments