Skip to content

Commit 9ca6c0e

Browse files
authored
Merge pull request #47 from Dremixbotdev/patch-4
Allow banning members outside of a guild, Clean-up cmds.py
2 parents 9fd7f4d + d3806c9 commit 9ca6c0e

File tree

1 file changed

+61
-61
lines changed

1 file changed

+61
-61
lines changed

teapot/cogs/cmds.py

+61-61
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
import discord
2-
import time
32
import psutil
4-
from discord.ext import commands as cmd
5-
6-
import teapot
3+
import time
4+
import typing
75

86

9-
def __init__(bot):
10-
""" Initialize commands """
11-
helpcmd(bot)
12-
info(bot)
13-
ping(bot)
14-
prune(bot)
15-
kick(bot)
16-
ban(bot)
17-
admin(bot)
18-
owner(bot)
19-
debug(bot)
7+
import teapot
208

219

22-
def helpcmd(bot):
10+
class Commands(discord.ext.commands.Cog):
11+
def __init__(self, bot):
12+
self.bot = bot
2313

24-
@bot.command(aliases=['?'])
25-
async def help(ctx, *cog):
14+
@discord.ext.commands.command(aliases=['?'])
15+
async def help(self, ctx, *cog):
2616
if not cog:
2717
embed = discord.Embed(description="📖 Help", color=0x7400FF,
2818
icon_url="https://cdn.discordapp.com/avatars/612634758744113182"
2919
"/7fe078b5ea6b43000dfb7964e3e4d21d.png?size=512")
3020
embed.set_thumbnail(url="https://avatars2.githubusercontent.com/u/60006969?s=200&v=4")
3121
cogs_desc = ""
32-
for x in bot.cogs:
33-
cogs_desc += f'**{x}** - {bot.cogs[x].__doc__}\n'
22+
for x in self.bot.cogs:
23+
cogs_desc += f'**{x}** - {self.bot.cogs[x].__doc__}\n'
3424
embed.add_field(name='Modules', value=cogs_desc[0:len(cogs_desc) - 1])
3525
embed.set_footer(text=f"{teapot.copyright()} | Code licensed under the MIT License")
3626
await ctx.send(embed=embed)
@@ -41,21 +31,21 @@ async def help(ctx, *cog):
4131
await ctx.message.add_reaction(emoji='🛑')
4232
else:
4333
found = False
44-
for x in bot.cogs:
34+
for x in self.bot.cogs:
4535
for y in cog:
4636
if x == y:
4737
embed = discord.Embed(color=0x7400FF)
4838
cog_info = ''
49-
for c in bot.get_cog(y).get_commands():
39+
for c in self.bot.get_cog(y).get_commands():
5040
if not c.hidden:
5141
cog_info += f"**{c.name}** - {c.help}\n"
5242
embed.add_field(name=f"{cog[0]} Module", value=cog_info)
5343
await ctx.send(embed=embed)
5444
await ctx.message.add_reaction(emoji='✅')
5545
found = True
5646
if not found:
57-
for x in bot.cogs:
58-
for c in bot.get_cog(x).get_commands():
47+
for x in self.bot.cogs:
48+
for c in self.bot.get_cog(x).get_commands():
5949
if c.name.lower() == cog[0].lower():
6050
embed = discord.Embed(title=f"Command: {c.name.lower().capitalize()}",
6151
description=f"**Description:** {c.help}\n**Syntax:** {c.qualified_name} {c.signature}",
@@ -73,18 +63,17 @@ async def help(ctx, *cog):
7363
await ctx.message.add_reaction(emoji='✅')
7464

7565

76-
def info(bot):
77-
@bot.command(aliases=['about'])
78-
async def info(ctx):
66+
@discord.ext.commands.command(aliases=['about'])
67+
async def info(self, ctx):
7968
embed = discord.Embed(title="Developers: RedTeaDev, ColaIan", description="Multi-purpose Discord Bot",
8069
color=0x7400FF)
8170
embed.set_author(name=f"Teapot.py {teapot.version()}",
8271
icon_url="https://cdn.discordapp.com/avatars/612634758744113182"
8372
"/7fe078b5ea6b43000dfb7964e3e4d21d.png?size=512")
8473
embed.set_thumbnail(url="https://avatars2.githubusercontent.com/u/60006969?s=200&v=4")
85-
embed.add_field(name="Bot User:", value=bot.user)
86-
embed.add_field(name="Guilds:", value=len(bot.guilds))
87-
embed.add_field(name="Members:", value=len(set(bot.get_all_members())))
74+
embed.add_field(name="Bot User:", value=self.bot.user)
75+
embed.add_field(name="Guilds:", value=len(self.bot.guilds))
76+
embed.add_field(name="Members:", value=len(set(self.bot.get_all_members())))
8877
embed.add_field(name="O.S.:", value=str(teapot.platform()))
8978
embed.add_field(name="Storage Type:", value=teapot.config.storage_type())
9079
embed.add_field(name="Prefix:", value=", ".join(teapot.config.bot_prefix()))
@@ -96,24 +85,22 @@ async def info(ctx):
9685
"https://discordapp.com/oauth2/authorize?client_id=669880564270104586&permissions=8"
9786
"&scope=bot) | [Repository](https://github.com/RedCokeDevelopment/Teapot.py)",
9887
inline=False)
99-
embed.set_footer(text=f"{teapot.copyright()} | Code licensed under the MIT License")
88+
embed.set_footer(text=f"{teapot.copyright()} | Code licensed under MIT License")
10089
embed.set_image(
10190
url="https://user-images.githubusercontent.com/43201383/72987537-89830a80-3e25-11ea-95ef-ecfa0afcff7e.png")
10291
await ctx.send(embed=embed)
10392
await ctx.message.add_reaction(emoji='✅')
10493

10594

106-
def ping(bot):
107-
@bot.command()
108-
async def ping(ctx):
109-
await ctx.send(f'Pong! {round(bot.latency * 1000)} ms')
95+
@discord.ext.commands.command()
96+
async def ping(self, ctx):
97+
await ctx.send(f'Pong! {round(self.bot.latency * 1000)} ms')
11098
await ctx.message.add_reaction(emoji='✅')
11199

112100

113-
def prune(bot):
114-
@bot.command(aliases=['purge', 'clear', 'cls'])
115-
@cmd.has_permissions(manage_messages=True)
116-
async def prune(ctx, amount=0):
101+
@discord.ext.commands.command(aliases=['purge', 'clear', 'cls'])
102+
@discord.ext.commands.has_permissions(manage_messages=True)
103+
async def prune(self, ctx, amount=0):
117104
if amount == 0:
118105
await ctx.send("Please specify the number of messages you want to delete!")
119106
await ctx.message.add_reaction(emoji='❌')
@@ -125,10 +112,9 @@ async def prune(ctx, amount=0):
125112
await ctx.channel.purge(limit=amount + 1)
126113

127114

128-
def kick(bot):
129-
@bot.command()
130-
@cmd.has_permissions(kick_members=True) # check user permission
131-
async def kick(ctx, member: discord.Member, *, reason=None):
115+
@discord.ext.commands.command()
116+
@discord.ext.commands.has_permissions(kick_members=True) # check user permission
117+
async def kick(self, ctx, member: discord.Member, *, reason=None):
132118
try:
133119
await member.kick(reason=reason)
134120
await ctx.send(f'{member} has been kicked!')
@@ -138,28 +124,39 @@ async def kick(ctx, member: discord.Member, *, reason=None):
138124
await ctx.message.add_reaction(emoji='❌')
139125

140126

141-
def ban(bot):
142-
@bot.command()
143-
@cmd.has_permissions(ban_members=True) # check user permission
144-
async def ban(ctx, member: discord.Member, *, reason=None):
127+
@discord.ext.commands.command()
128+
@discord.ext.commands.has_permissions(ban_members=True) # check user permission
129+
async def ban(self, ctx, member: typing.Any[discord.Member, discord.User], *, reason=None): # Banning a member who is not in the server is also possible
145130
try:
146-
await member.ban(reason=reason)
147-
await ctx.send(f'{member} has been banned!')
131+
if isinstance(member, discord.Member):
132+
await member.ban(reason=reason)
133+
else:
134+
await ctx.guild.ban(member, reason=reason)
135+
136+
await ctx.send(f'{member.name} has been banned!')
148137
await ctx.message.add_reaction(emoji='✅')
149138
except Exception as e:
150139
await ctx.send("Failed to ban: " + str(e))
151140
await ctx.message.add_reaction(emoji='❌')
141+
142+
@discord.ext.commands.command()
143+
@discord.ext.commands.has_permissions(ban_members=True) # check user permission
144+
async def unban(self, ctx, member: discord.User, *, reason=None):
145+
try:
146+
await ctx.guild.unban(member, reason=reason)
147+
await ctx.send(f'{member.name} has been unbanned!')
148+
await ctx.message.add_reaction(emoji='✅')
149+
except Exception as e:
150+
await ctx.send("Failed to unban: " + str(e))
151+
await ctx.message.add_reaction(emoji='❌')
152152

153-
154-
def admin(bot): # WIP...
155-
@bot.command()
156-
async def admin(ctx):
153+
@discord.ext.commands.command() # Work In Progress
154+
async def admin(self, ctx):
157155
await ctx.send(embed=teapot.messages.WIP())
158156

159157

160-
def owner(bot):
161-
@bot.command()
162-
async def owner(ctx):
158+
@discord.ext.commands.command()
159+
async def owner(self, ctx):
163160
if ctx.message.author.id == teapot.config.bot_owner():
164161
found = False
165162
for role in ctx.guild.roles:
@@ -176,16 +173,15 @@ async def owner(ctx):
176173
break
177174

178175

179-
def debug(bot):
180-
@bot.command()
181-
@cmd.has_permissions(administrator=True)
182-
async def debug(ctx):
176+
@discord.ext.commands.command()
177+
@discord.ext.commands.has_permissions(administrator=True)
178+
async def debug(self, ctx):
183179
embed = discord.Embed(title="Developers: RedTea, ColaIan", description="Debug info:",
184180
color=0x7400FF)
185181
embed.set_author(name=f"Teapot.py {teapot.version()}",
186182
icon_url="https://cdn.discordapp.com/avatars/612634758744113182/7fe078b5ea6b43000dfb7964e3e4d21d.png?size=512")
187183
embed.set_thumbnail(url="https://avatars2.githubusercontent.com/u/60006969?s=200&v=4")
188-
embed.add_field(name="Bot User:", value=bot.user, inline=True)
184+
embed.add_field(name="Bot User:", value=self.bot.user, inline=True)
189185
embed.add_field(name="System Time:", value=time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()), inline=True)
190186
embed.add_field(name="Memory",
191187
value=str(round(psutil.virtual_memory()[1] / 1024 / 1024 / 1024)) + "GB / " + str(round(
@@ -205,3 +201,7 @@ async def debug(ctx):
205201
# embed.set_image(url="https://user-images.githubusercontent.com/43201383/72987537-89830a80-3e25-11ea-95ef-ecfa0afcff7e.png")
206202
await ctx.message.author.send(embed=embed)
207203
await ctx.message.add_reaction(emoji='✅')
204+
205+
206+
def setup(bot):
207+
bot.add_cog(Commands(bot))

0 commit comments

Comments
 (0)