-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathgeneral.py
More file actions
327 lines (293 loc) 路 11.5 KB
/
Copy pathgeneral.py
File metadata and controls
327 lines (293 loc) 路 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""
Copyright 漏 Krypton 2019-Present - https://github.com/kkrypt0nn (https://krypton.ninja)
Description:
馃悕 A simple template to start to code your own and personalized Discord bot in Python
Version: 6.5.0
"""
import platform
import random
import aiohttp
import discord
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
class FeedbackForm(discord.ui.Modal, title="Feeedback"):
feedback = discord.ui.TextInput(
label="What do you think about this bot?",
style=discord.TextStyle.long,
placeholder="Type your answer here...",
required=True,
max_length=256,
)
async def on_submit(self, interaction: discord.Interaction):
self.interaction = interaction
self.answer = str(self.feedback)
self.stop()
class General(commands.Cog, name="general"):
def __init__(self, bot) -> None:
self.bot = bot
self.context_menu_user = app_commands.ContextMenu(
name="Grab ID", callback=self.grab_id
)
self.bot.tree.add_command(self.context_menu_user)
self.context_menu_message = app_commands.ContextMenu(
name="Remove spoilers", callback=self.remove_spoilers
)
self.bot.tree.add_command(self.context_menu_message)
# Message context menu command
async def remove_spoilers(
self, interaction: discord.Interaction, message: discord.Message
) -> None:
"""
Removes the spoilers from the message. This command requires the MESSAGE_CONTENT intent to work properly.
:param interaction: The application command interaction.
:param message: The message that is being interacted with.
"""
spoiler_attachment = None
for attachment in message.attachments:
if attachment.is_spoiler():
spoiler_attachment = attachment
break
embed = discord.Embed(
title="Message without spoilers",
description=message.content.replace("||", ""),
color=0xBEBEFE,
)
if spoiler_attachment is not None:
embed.set_image(url=attachment.url)
await interaction.response.send_message(embed=embed, ephemeral=True)
# User context menu command
async def grab_id(
self, interaction: discord.Interaction, user: discord.User
) -> None:
"""
Grabs the ID of the user.
:param interaction: The application command interaction.
:param user: The user that is being interacted with.
"""
embed = discord.Embed(
description=f"The ID of {user.mention} is `{user.id}`.",
color=0xBEBEFE,
)
await interaction.response.send_message(embed=embed, ephemeral=True)
@commands.hybrid_command(
name="help", description="List all commands the bot has loaded."
)
async def help(self, context: Context) -> None:
embed = discord.Embed(
title="Help", description="List of available commands:", color=0xBEBEFE
)
for i in self.bot.cogs:
if i == "owner" and not (await self.bot.is_owner(context.author)):
continue
cog = self.bot.get_cog(i.lower())
commands = cog.get_commands()
data = []
for command in commands:
description = command.description.partition("\n")[0]
data.append(f"{command.name} - {description}")
help_text = "\n".join(data)
embed.add_field(
name=i.capitalize(), value=f"```{help_text}```", inline=False
)
await context.send(embed=embed)
@commands.hybrid_command(
name="botinfo",
description="Get some useful (or not) information about the bot.",
)
async def botinfo(self, context: Context) -> None:
"""
Get some useful (or not) information about the bot.
:param context: The hybrid command context.
"""
embed = discord.Embed(
description="Used [Krypton's](https://krypton.ninja) template",
color=0xBEBEFE,
)
embed.set_author(name="Bot Information")
embed.add_field(name="Owner:", value="Krypton#7331", inline=True)
embed.add_field(
name="Python Version:", value=f"{platform.python_version()}", inline=True
)
embed.add_field(
name="Prefix:",
value=f"/ (Slash Commands) or {self.bot.bot_prefix} for normal commands",
inline=False,
)
embed.set_footer(text=f"Requested by {context.author}")
await context.send(embed=embed)
@commands.hybrid_command(
name="serverinfo",
description="Get some useful (or not) information about the server.",
)
async def serverinfo(self, context: Context) -> None:
"""
Get some useful (or not) information about the server.
:param context: The hybrid command context.
"""
roles = [role.name for role in context.guild.roles]
num_roles = len(roles)
if num_roles > 50:
roles = roles[:50]
roles.append(f">>>> Displaying [50/{num_roles}] Roles")
roles = ", ".join(roles)
embed = discord.Embed(
title="**Server Name:**", description=f"{context.guild}", color=0xBEBEFE
)
if context.guild.icon is not None:
embed.set_thumbnail(url=context.guild.icon.url)
embed.add_field(name="Server ID", value=context.guild.id)
embed.add_field(name="Member Count", value=context.guild.member_count)
embed.add_field(
name="Text/Voice Channels", value=f"{len(context.guild.channels)}"
)
embed.add_field(name=f"Roles ({len(context.guild.roles)})", value=roles)
embed.set_footer(text=f"Created at: {context.guild.created_at}")
await context.send(embed=embed)
@commands.hybrid_command(
name="ping",
description="Check if the bot is alive.",
)
async def ping(self, context: Context) -> None:
"""
Check if the bot is alive.
:param context: The hybrid command context.
"""
embed = discord.Embed(
title="馃彄 Pong!",
description=f"The bot latency is {round(self.bot.latency * 1000)}ms.",
color=0xBEBEFE,
)
await context.send(embed=embed)
@commands.hybrid_command(
name="invite",
description="Get the invite link of the bot to be able to invite it.",
)
async def invite(self, context: Context) -> None:
"""
Get the invite link of the bot to be able to invite it.
:param context: The hybrid command context.
"""
embed = discord.Embed(
description=f"Invite me by clicking [here]({self.bot.invite_link}).",
color=0xD75BF4,
)
try:
await context.author.send(embed=embed)
await context.send("I sent you a private message!")
except discord.Forbidden:
await context.send(embed=embed)
@commands.hybrid_command(
name="server",
description="Get the invite link of the discord server of the bot for some support.",
)
async def server(self, context: Context) -> None:
"""
Get the invite link of the discord server of the bot for some support.
:param context: The hybrid command context.
"""
embed = discord.Embed(
description=f"Join the support server for the bot by clicking [here](https://discord.gg/mTBrXyWxAF).",
color=0xD75BF4,
)
try:
await context.author.send(embed=embed)
await context.send("I sent you a private message!")
except discord.Forbidden:
await context.send(embed=embed)
@commands.hybrid_command(
name="8ball",
description="Ask any question to the bot.",
)
@app_commands.describe(question="The question you want to ask.")
async def eight_ball(self, context: Context, *, question: str) -> None:
"""
Ask any question to the bot.
:param context: The hybrid command context.
:param question: The question that should be asked by the user.
"""
answers = [
"It is certain.",
"It is decidedly so.",
"You may rely on it.",
"Without a doubt.",
"Yes - definitely.",
"As I see, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again later.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
]
embed = discord.Embed(
title="**My Answer:**",
description=f"{random.choice(answers)}",
color=0xBEBEFE,
)
embed.set_footer(text=f"The question was: {question}")
await context.send(embed=embed)
@commands.hybrid_command(
name="bitcoin",
description="Get the current price of bitcoin.",
)
async def bitcoin(self, context: Context) -> None:
"""
Get the current price of bitcoin.
:param context: The hybrid command context.
"""
# This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request
async with aiohttp.ClientSession() as session:
async with session.get(
"https://api.coindesk.com/v1/bpi/currentprice/BTC.json"
) as request:
if request.status == 200:
data = await request.json()
embed = discord.Embed(
title="Bitcoin price",
description=f"The current price is {data['bpi']['USD']['rate']} :dollar:",
color=0xBEBEFE,
)
else:
embed = discord.Embed(
title="Error!",
description="There is something wrong with the API, please try again later",
color=0xE02B2B,
)
await context.send(embed=embed)
@app_commands.command(
name="feedback", description="Submit a feedback for the owners of the bot"
)
async def feedback(self, interaction: discord.Interaction) -> None:
"""
Submit a feedback for the owners of the bot.
:param context: The hybrid command context.
"""
feedback_form = FeedbackForm()
await interaction.response.send_modal(feedback_form)
await feedback_form.wait()
interaction = feedback_form.interaction
await interaction.response.send_message(
embed=discord.Embed(
description="Thank you for your feedback, the owners have been notified about it.",
color=0xBEBEFE,
)
)
app_owner = (await self.bot.application_info()).owner
await app_owner.send(
embed=discord.Embed(
title="New Feedback",
description=f"{interaction.user} (<@{interaction.user.id}>) has submitted a new feedback:\n```\n{feedback_form.answer}\n```",
color=0xBEBEFE,
)
)
async def setup(bot) -> None:
await bot.add_cog(General(bot))