Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Stats (#13)
Browse files Browse the repository at this point in the history
* sync utilities

* split some utils from tag cogs

* splitting help and event to each cogs

* [pre-commit] auto fixes

Co-authored-by: clemiee <clemiee@users.noreply.github.com>
  • Loading branch information
Clemie McCartney and clemiee authored Sep 8, 2022
1 parent da48c87 commit 451b6d4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 100 deletions.
76 changes: 76 additions & 0 deletions extensions/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import datetime
import os

import aiohttp
from dotenv import load_dotenv
from naff import (
Activity,
ActivityType,
Embed,
Extension,
IntervalTrigger,
Status,
Task,
listen,
)
from naff.api.events.discord import GuildJoin, GuildLeft

from core.base import CustomClient
from utilities.events import *

load_dotenv()


class events(Extension):
bot: CustomClient

@listen()
async def on_guild_join(self, event: GuildJoin):
if self.bot.is_ready:
guild = event.guild
e = Embed(color=0x53DDA4, title="Joined a Guild")
await send_guild_stats(self, e, guild, 997921447701921953)

@listen()
async def on_guild_left(self, event: GuildLeft):
guild = event.guild
e = Embed(color=0x53DDA4, title="Left a Guild")
await send_guild_stats(self, e, guild, 997921473861799976)

@Task.create(IntervalTrigger(seconds=30))
async def presence_changes(self):
await self.bot.change_presence(
status=Status.ONLINE,
activity=Activity(
name=f"{len(self.bot.guilds)} servers | /help",
type=ActivityType.COMPETING,
),
)

@listen()
async def on_startup(self):
"""Gets triggered on startup"""

self.presence_changes.start()

@Task.create(IntervalTrigger(minutes=30))
async def upload_stats(self):
payload = {
"server_count": len(self.bot.guilds),
}
headers = {
"Authorization": str(os.getenv("TOPGG_TOKEN")),
"Content-Type": "application/json",
}
async with aiohttp.ClientSession() as session:
await session.post(
f"https://top.gg/api/bots/{self.bot.user.id}/stats",
json=payload,
headers=headers,
)


def setup(bot: CustomClient):
"""Let naff load the extension"""

events(bot)
88 changes: 3 additions & 85 deletions extensions/stats.py → extensions/help.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
import datetime
import os

import aiohttp
import psutil
from dotenv import load_dotenv
from naff import (
Activity,
ActivityType,
Embed,
Extension,
InteractionContext,
IntervalTrigger,
OptionTypes,
Status,
Task,
listen,
slash_command,
slash_option,
)
from naff.api.events.discord import GuildJoin, GuildLeft
from naff import Embed, Extension, InteractionContext, slash_command, slash_option
from naff.ext.paginators import Paginator

from core.base import CustomClient
from utilities.uptime import *

load_dotenv()


class stats(Extension):
class help(Extension):
bot: CustomClient

def __init__(self, bot):
Expand Down Expand Up @@ -279,71 +260,8 @@ async def help(self, ctx: InteractionContext):
)
return await paginators.send(ctx)

async def send_guild_stats(self, e, guild, r_channel):
owner = await self.bot.fetch_user(guild._owner_id)
e.add_field(name="Name", value=guild.name)
e.add_field(name="ID", value=guild.id)
e.add_field(name="Owner", value=f"{owner.mention} (ID: {owner.id})")

total = guild.member_count
e.add_field(name="Members", value=str(total))

if guild.icon:
e.set_thumbnail(url=guild.icon.url)

if guild.me:
e.timestamp = guild.me.joined_at

ch = self.bot.get_channel(r_channel)
await ch.send(embed=e)

@listen()
async def on_guild_join(self, event: GuildJoin):
if self.bot.is_ready:
guild = event.guild
e = Embed(color=0x53DDA4, title="Joined a Guild")
await self.send_guild_stats(e, guild, 997921447701921953)

@listen()
async def on_guild_left(self, event: GuildLeft):
guild = event.guild
e = Embed(color=0x53DDA4, title="Left a Guild")
await self.send_guild_stats(e, guild, 997921473861799976)

@Task.create(IntervalTrigger(seconds=30))
async def presence_changes(self):
await self.bot.change_presence(
status=Status.ONLINE,
activity=Activity(
name=f"{len(self.bot.guilds)} servers | /help",
type=ActivityType.COMPETING,
),
)

@listen()
async def on_startup(self):
"""Gets triggered on startup"""

self.presence_changes.start()

@Task.create(IntervalTrigger(minutes=30))
async def upload_stats(self):
payload = {
"server_count": len(self.bot.guilds),
}
headers = {
"Authorization": str(os.getenv("TOPGG_TOKEN")),
"Content-Type": "application/json",
}
async with aiohttp.ClientSession() as session:
await session.post(
f"https://top.gg/api/bots/{self.bot.user.id}/stats",
json=payload,
headers=headers,
)


def setup(bot: CustomClient):
"""Let naff load the extension"""

stats(bot)
help(bot)
14 changes: 0 additions & 14 deletions extensions/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@
tags = cluster["madeline"]["tags"]


def geturl(string):
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex, string)
return [x[0] for x in url]


def find_member(ctx, userid):
members = [m for m in ctx.guild.members if m.id == userid]
if members != []:
for m in members:
return m
return None


class Tags(Extension):
def __init__(self, bot: Client):
self.bot = bot
Expand Down
29 changes: 28 additions & 1 deletion utilities/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,37 @@ async def check(ctx: Context) -> bool:

def is_owner():
"""
Is the author the owner of the bot.
Is the author the owner of the bot?
"""

async def check(ctx: Context) -> bool:
return ctx.author.id == 351150966948757504

return check


def geturl(string):
"""
Check if message has any links.
Args:
*string: The messages to check for
"""
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex, string)
return [x[0] for x in url]


def find_member(ctx, userid):
"""
Double check if member is still in that guild or not.
Args:
ctx: Pass the Context
*userid: The user id to check for
"""
members = [m for m in ctx.guild.members if m.id == userid]
if members != []:
for m in members:
return m
return None
17 changes: 17 additions & 0 deletions utilities/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
async def send_guild_stats(self, e, guild, r_channel):
owner = await self.bot.fetch_user(guild._owner_id)
e.add_field(name="Name", value=guild.name)
e.add_field(name="ID", value=guild.id)
e.add_field(name="Owner", value=f"{owner.mention} (ID: {owner.id})")

total = guild.member_count
e.add_field(name="Members", value=str(total))

if guild.icon:
e.set_thumbnail(url=guild.icon.url)

if guild.me:
e.timestamp = guild.me.joined_at

ch = self.bot.get_channel(r_channel)
await ch.send(embed=e)

0 comments on commit 451b6d4

Please sign in to comment.