Skip to content

Commit 44a53a3

Browse files
committed
initial migration to pycord
1 parent 29c7e38 commit 44a53a3

File tree

11 files changed

+156
-142
lines changed

11 files changed

+156
-142
lines changed

bot.py

+56-48
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
import io
1212
import sr_api
1313
import humanize
14-
import aiohttp
1514
import asyncpg
1615
import ext.helpers as helpers
1716
from PIL import Image, ImageDraw, ImageFont, ImageFilter
1817
from dotenv import load_dotenv
1918
from discord.ext import commands, tasks
20-
from discord_slash import SlashCommand, SlashContext
21-
from dislash import SlashClient
2219

2320

2421
async def prefix(bot_, message):
@@ -30,7 +27,7 @@ class CustomHelp(commands.HelpCommand):
3027

3128
def get_ending_note(self):
3229
return 'Use {0}{1} [command] for more info on a command.'.format(
33-
self.clean_prefix, self.invoked_with)
30+
self.context.clean_prefix, self.invoked_with)
3431

3532
def get_command_signature(self, command):
3633
parent = command.full_parent_name
@@ -90,9 +87,9 @@ async def send_group_help(self, group):
9087
embed.set_footer(text=self.get_ending_note())
9188
await self.get_destination().send(embed=embed)
9289

93-
# This makes it so it uses the function above
90+
# This makes it use the function above
9491
# Less work for us to do since they're both similar.
95-
# If you want to make regular command help look different then override it
92+
# If you want to make regular command help look different, then override it
9693
send_command_help = send_group_help
9794

9895

@@ -110,7 +107,7 @@ async def send_group_help(self, group):
110107
init_data = helpers.storage(bot)
111108

112109

113-
class pools:
110+
class Pools:
114111
config = asyncpg.create_pool(database='codingbot_db',
115112
init=helpers.init_connection)
116113

@@ -119,7 +116,7 @@ class pools:
119116
bot.tracker = DiscordUtils.InviteTracker(bot)
120117
bot.default_prefixes = [',']
121118
bot.server_cache = {}
122-
bot.pools = pools
119+
bot.pools = Pools
123120
bot.owner_id = None
124121
bot.owner_ids = init_data['owners']
125122
bot.blacklisted = init_data['blacklisted']
@@ -129,8 +126,6 @@ class pools:
129126
bot.sr_api = sr_api.Client()
130127
bot.sr_api_premium = False
131128
bot.processing_commands = 0
132-
bot.slash = SlashCommand(bot, sync_commands=True)
133-
bot.dislash = SlashClient(bot)
134129
for cog in bot.active_cogs:
135130
try:
136131
bot.load_extension(cog)
@@ -148,7 +143,8 @@ async def on_message(message):
148143
ctx = await bot.get_context(message, cls=helpers.Context)
149144
await bot.invoke(ctx)
150145
for prefix_ in await prefix(bot, message):
151-
if message.content.startswith(f'\\{prefix_}') and bot.get_command(message.content.split()[0][len(prefix_) + 1:]):
146+
if (message.content.startswith(f'\\{prefix_}')
147+
and bot.get_command(message.content.split()[0][len(prefix_) + 1:])):
152148
return await message.channel.send('lol')
153149

154150

@@ -191,31 +187,31 @@ async def on_member_join(member):
191187
if not member.guild.id == 681882711945641997:
192188
return
193189
if not member.name.isalnum():
194-
await member.edit(nick=unicodedata.normalize('NFKD',member.name))
190+
await member.edit(nick=unicodedata.normalize('NFKD', member.name))
195191
if member.bot:
196192
channel = member.guild.get_channel(743817386792058971)
197193
return await channel.send(content=f'Bot added: {member.mention}')
198194
inviter = await bot.tracker.fetch_inviter(member)
199195
rules = member.guild.rules_channel.mention
200196
embed = discord.Embed(
201-
title='Welcome to The Coding Realm!',
197+
title=f'Welcome to {member.guild.name}!',
202198
description=(
203199
f'Welcome {member.mention}, we\'re glad you joined! Before you get'
204200
' started, here are some things to check out: \n**Read the Rules:'
205201
f'** {rules} \n**Get roles:** <#726074137168183356> and '
206202
'<#806909970482069556> \n**Want help? Read here:** '
207203
'<#799527165863395338> and <#754712400757784709>'),
208-
timestamp=datetime.datetime.utcnow())
209-
ago = datetime.datetime.utcnow() - member.created_at
210-
img = io.BytesIO(await member.avatar_url_as(format='png', size=128).read())
204+
timestamp=datetime.datetime.now(datetime.timezone.utc))
205+
ago = datetime.datetime.now(datetime.timezone.utc) - member.created_at
206+
img = io.BytesIO(await member.avatar.with_format("png").with_size(128).read())
211207
try:
212-
img2 = io.BytesIO(await member.guild.banner_url_as(format='png', size=512).read())
208+
img2 = io.BytesIO(await member.guild.banner.with_format("png").with_size(512).read())
213209
except:
214210
img2 = 'storage/banner.png'
215211
base = Image.open(img).convert("RGBA")
216212
base = base.resize((128, 128))
217213
txt = Image.open(img2).convert("RGBA")
218-
txt = txt.point(lambda p: p * 0.5)
214+
txt = txt.point(lambda p: int(p * 0.5))
219215
txt = txt.resize((512, 200))
220216
d = ImageDraw.Draw(txt)
221217
fill = (255, 255, 255, 255)
@@ -243,7 +239,7 @@ async def on_member_join(member):
243239
try:
244240
invite = await member.guild.vanity_invite()
245241
text = f'• Joined using vanity invite: {invite.code} ({invite.uses} uses)'
246-
except discord.errors.HTTPException:
242+
except discord.HTTPException:
247243
text = 'I couldn\'t find who invited them'
248244
d.text(((txt.size[0] // 8) * 3, (txt.size[1] // 16) * 9), text, font=font, fill=fill, align='center')
249245
text = f'• Account created: {humanize.naturaldelta(ago)} ago'
@@ -265,7 +261,10 @@ async def on_member_join(member):
265261
channel = member.guild.get_channel(743817386792058971)
266262
await channel.send(content=member.mention, file=file)
267263
verify_here = member.guild.get_channel(759220767711297566)
268-
await verify_here.send(f'Welcome {member.mention}! Follow the instructions in other channels to get verified. :)', embed=embed)
264+
await verify_here.send(f'Welcome {member.mention}! Follow the instructions in other channels to get verified. :)',
265+
embed=embed)
266+
267+
269268
# try:
270269
# await member.send(embed=embed)
271270
# except discord.errors.Forbidden:
@@ -292,7 +291,7 @@ async def on_command_error(ctx, error):
292291
commands.MissingPermissions, commands.MaxConcurrencyReached))):
293292
try:
294293
await ctx.reinvoke()
295-
except discord.ext.commands.CommandError as e:
294+
except discord.ext.commands.CommandError:
296295
pass
297296
else:
298297
return
@@ -319,10 +318,10 @@ async def on_command_error(ctx, error):
319318
if not isinstance(error, commands.CommandNotFound):
320319
embed = ctx.embed(title="Error", description=text,
321320
color=discord.Color.red())
322-
embed.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
321+
embed.set_author(name=ctx.author, icon_url=ctx.author.avatar.url)
323322
owner = bot.get_user(ctx.bot.owner_ids[0])
324323
embed.set_footer(
325-
icon_url=bot.user.avatar_url,
324+
icon_url=bot.user.avatar.url,
326325
text=f'If you think this is a mistake please contact {owner}')
327326
await ctx.send(embed=embed)
328327

@@ -333,10 +332,10 @@ async def on_command_error(ctx, error):
333332
f'{humanize.precisedelta(time)}')
334333
embed = ctx.embed(title="Error", description=error,
335334
color=discord.Color.red())
336-
embed.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
335+
embed.set_author(name=ctx.author, icon_url=ctx.author.avatar.url)
337336
owner = bot.get_user(ctx.bot.owner_ids[0])
338337
embed.set_footer(
339-
icon_url=bot.user.avatar_url,
338+
icon_url=bot.user.avatar.url,
340339
text=f'If you think this is a mistake please contact {owner}')
341340
await ctx.send(embed=embed)
342341

@@ -347,7 +346,7 @@ async def on_command_error(ctx, error):
347346
'but if it continues to occur please DM '
348347
f'<@{ctx.bot.owner_ids[0]}>'), color=discord.Color.red())
349348
await ctx.send(embed=embed)
350-
except discord.errors.Forbidden:
349+
except discord.Forbidden:
351350
pass
352351
await helpers.log_command_error(ctx, exception, False)
353352

@@ -451,27 +450,33 @@ async def _reloadall(self, ctx):
451450

452451
@bot.before_invoke
453452
async def before_invoke(ctx):
454-
bot.processing_commands += 1
453+
ctx.bot.processing_commands += 1
455454

456455

457456
@bot.after_invoke
458457
async def after_invoke(ctx):
459-
bot.processing_commands -= 1
458+
ctx.bot.processing_commands -= 1
460459

461460

462461
@tasks.loop(minutes=2)
463462
async def status_change():
464-
statuses = ['over TCR', 'you', 'swas', '@everyone', 'general chat', 'discord', ',help', 'your mom',
465-
'bob and shadow argue', 'swas simp for false', 'new members', 'the staff team',
466-
random.choice(bot.get_guild(681882711945641997).get_role(795145820210462771).members).name,
467-
'helpers', 'code', 'mass murders', 'karen be an idiot', 'a video', 'watches', 'bob',
468-
'fight club', 'youtube', 'https://devbio.me/u/CodingBot', 'potatoes', 'simps', 'people', 'my server',
469-
'humans destroy the world', 'AI take over the world', 'female bots 😳', 'dinosaurs',
470-
'https://youtu.be/dQw4w9WgXcQ', 'idiots', 'the beginning of WWIII', 'verified bot tags with envy',
471-
random.choice(bot.get_guild(681882711945641997).get_role(737517726737629214).members).name +
472-
' (Server Booster)', 'Server Boosters (boost to get your name on here)', 'OG members',
473-
"dalek rising from the ashes", 'spongebob', 'turtles', 'SQUIRREL!!!', 'people get banned', 'por...k chops',
474-
'my poggers discriminator', 'tux', 'linux overcome windows', 'bob get a gf', 'a documentary']
463+
464+
statuses = ['over TCR', 'you', 'swas', '@everyone', 'general chat', 'discord', ',help', 'your mom',
465+
'bob and shadow argue', 'swas simp for false', 'new members', 'the staff team', 'helpers', 'code',
466+
'mass murders', 'karen be an idiot', 'a video', 'watches', 'bob', 'fight club', 'youtube',
467+
'https://devbio.me/u/CodingBot', 'potatoes', 'simps', 'people', 'my server', 'humans destroy the world',
468+
'AI take over the world', 'female bots 😳', 'dinosaurs', 'https://youtu.be/dQw4w9WgXcQ', 'idiots',
469+
'the beginning of WWIII', 'verified bot tags with envy',
470+
'Server Boosters (boost to get your name on here)', 'OG members', "dalek rising from the ashes",
471+
'spongebob', 'turtles', 'SQUIRREL!!!', 'people get banned', 'por...k chops', 'my poggers discriminator',
472+
'tux', 'linux overcome windows', 'bob get a gf', 'a documentary']
473+
tcr = bot.get_guild(681882711945641997)
474+
if tcr:
475+
if tcr.get_role(795145820210462771):
476+
statuses.append(random.choice(tcr.get_role(795145820210462771).members).name)
477+
if tcr.get_role(737517726737629214):
478+
statuses.append(random.choice(tcr.get_role(737517726737629214).members).name + ' (Server Booster)')
479+
475480
await bot.change_presence(activity=discord.Activity(
476481
type=discord.ActivityType.watching,
477482
name=random.choice(
@@ -489,14 +494,16 @@ async def before_status_change():
489494
@tasks.loop(minutes=5)
490495
async def booster_perms():
491496
guild = bot.get_guild(681882711945641997)
497+
if not guild:
498+
return
492499
nitro_booster = guild.get_role(737517726737629214)
493500
active = guild.get_role(726029173067481129)
494501
muted = guild.get_role(766469426429820949)
495502
for member in nitro_booster.members:
496503
if not (active in member.roles or muted in member.roles):
497504
try:
498505
await member.add_roles(active)
499-
except discord.errors.Forbidden:
506+
except discord.Forbidden:
500507
pass
501508

502509

@@ -523,20 +530,21 @@ async def disabled_command(ctx):
523530
or ctx.author.id == ctx.guild.owner.id)
524531

525532

526-
@bot.slash.slash(name='help', description='Get the help for the bot')
527-
async def slash_help(ctx: SlashContext):
528-
await ctx.send(embeds=[discord.Embed(title='Hello There!', description=(
533+
@bot.slash_command(name='help', description='Get the help for the bot')
534+
async def slash_help(ctx):
535+
await ctx.respond(embed=discord.Embed(title='Hello There!', description=(
529536
'I use special command prefixes for my commands. Please type \n'
530-
f'{bot.user.mention + " help"} \nfor my full help menu!'))])
537+
f'{bot.user.mention + " help"} \nfor my full help menu!')))
531538

532539

533-
@bot.slash.slash(name='invite', description='Invite the bot to your server')
534-
async def slash_invite(ctx: SlashContext):
540+
@bot.slash_command(name='invite', description='Invite the bot to your server')
541+
async def slash_invite(ctx):
535542
embed = discord.Embed(title='Invite', description=(
536543
'[Click Here](https://discord.com/oauth2/authorize?client_id='
537544
f'{bot.user.id}&permissions=8&scope=bot%20applications.commands) '
538-
'to invite me!'), timestamp=datetime.datetime.utcnow())
539-
await ctx.send(embeds=[embed])
545+
'to invite me!'), timestamp=datetime.datetime.now(datetime.timezone.utc))
546+
await ctx.respond(embed=embed)
547+
540548

541549
status_change.start()
542550
booster_perms.start()

0 commit comments

Comments
 (0)