Skip to content

Commit

Permalink
have the bot listen to some chuni originals
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-psi committed Nov 30, 2024
1 parent 9146d58 commit 8c0a954
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Binary file modified assets/b30_bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def setup_database(conn, _):
kamaitachi_client_id=config.credentials.kamaitachi_client_id,
kamaitachi_client_secret=config.credentials.kamaitachi_client_secret,
)
_ = asyncio.ensure_future(
_ = asyncio.ensure_future( # noqa: RUF006
web._run_app(
self.app,
port=config.web.port,
Expand Down
37 changes: 34 additions & 3 deletions cogs/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import discord
import tomllib
from discord.ext import commands
from discord.ext import commands, tasks
from discord.ext.commands import Context, Greedy
from discord.utils import oauth_url
from sqlalchemy import delete, func, select

from database.models import Cookie, Prefix
from database.models import Cookie, Prefix, Song
from utils.config import config
from utils.constants import VERSION_NAMES

Expand All @@ -26,6 +26,12 @@ def __init__(self, bot: "ChuniBot") -> None:
self.bot = bot
self.utils: "UtilsCog" = self.bot.get_cog("Utils") # type: ignore[reportGeneralTypeIssues]

async def cog_load(self) -> None:
self.listening.start()

async def cog_unload(self) -> None:
self.listening.stop()

@commands.command("treesync", hidden=True, invoke_without_command=True)
@commands.is_owner()
async def sync(
Expand Down Expand Up @@ -156,7 +162,7 @@ async def botinfo(self, ctx: Context):
embed.add_field(name="Uptime", value=f"<t:{int(self.bot.launch_time)}:R>")
embed.add_field(name="Total servers", value=len(self.bot.guilds))
embed.add_field(name="Total users", value=str(users))
embed.add_field(name="\u200B", value="\u200B")
embed.add_field(name="\u200b", value="\u200b")

await ctx.reply(embed=embed, mention_author=False)

Expand Down Expand Up @@ -238,6 +244,31 @@ async def legal(self, ctx: Context):
mention_author=False,
)

@tasks.loop(minutes=3)
async def listening(self):
async with self.bot.begin_db_session() as session:
query = (
select(Song)
.where(Song.genre == "ORIGINAL")
.order_by(func.random())
.limit(1)
)
song = await session.scalar(query)

if song is None:
return

await self.bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.listening,
name=f"{song.artist} - {song.title}",
)
)

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


async def setup(bot: "ChuniBot") -> None:
await bot.add_cog(MiscCog(bot))
Expand Down

0 comments on commit 8c0a954

Please sign in to comment.