Skip to content

Commit

Permalink
This should theoretically work
Browse files Browse the repository at this point in the history
  • Loading branch information
tookender committed Sep 3, 2023
1 parent cf89085 commit c9b4303
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
7 changes: 6 additions & 1 deletion data/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ CREATE TABLE IF NOT EXISTS guilds (
levelling_message TEXT DEFAULT '**GG**, **{user}** has reached level **{level}**!',
levelling_multiplier FLOAT DEFAULT 1.0,
levelling_delete_after INT DEFAULT NULL
);
);

CREATE TABLE IF NOT EXISTS avatars (
user_id BIGINT PRIMARY KEY,
avatar_url TEXT NOT NULL
)
3 changes: 2 additions & 1 deletion extensions/events/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .guild import GuildCog
from .ping import PingCog
from .avatars import AvatarsCog


class Events(PingCog, GuildCog): # PingCog needs to be first, it adds attributes
class Events(AvatarsCog, PingCog, GuildCog): # PingCog needs to be first, it adds attributes
pass


Expand Down
45 changes: 45 additions & 0 deletions extensions/events/avatars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import contextlib
import random

import discord
from discord.ext import commands, tasks

from utils import Embed, Korii
import logging


class AvatarsCog(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
self.update_avatars.start()
self.rustbyte_guild: discord.Guild
self.developers_role: discord.Role

def cog_unload(self):
self.update_avatars.cancel()

@tasks.loop(minutes=5)
async def update_avatars(self):
for member in self.rustbyte_guild.members:
if self.developers_role in member.roles:
assert member.avatar
await self.bot.pool.execute("INSERT INTO avatars(user_id, avatar_url) VALUES ($1, $2) ON CONFLICT(user_id) DO UPDATE SET avatar_url = $2",
member.id, member.avatar.replace(size=256, format="webp"))

@update_avatars.before_loop
async def wait_until_ready(self):
await self.bot.wait_until_ready()

guild = self.bot.get_guild(1096226282292920403)
if not isinstance(guild, discord.Guild):
logging.error("Cannot find Rustbyte guild")
return

self.rustbyte_guild = guild

role = guild.get_role(1096354583888003102)
if not isinstance(role, discord.Role):
logging.error("Cannot find Developers role in Rustbyte guild")
return

self.developers_role = role

0 comments on commit c9b4303

Please sign in to comment.