Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Periodic gam coins #40

Merged
merged 5 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/chatbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@

from discord import GroupChannel, RawReactionActionEvent, TextChannel
from discord.ext.commands import Bot, Context
from discord.flags import Intents
from discord.partial_emoji import PartialEmoji
from django.conf import settings

from .models import EmojiScore, GamUser, Prediction, PredictionChoice, Wager
from .checks import is_in_channel, is_local_command
from .tasks import UserPresenceDetectorCog


logger = getLogger(__name__)

bot = Bot(command_prefix="!")
# This will enable everything, Intents.default() just calls .all() and disables
# presences and members which we need
intents = Intents.all()
bot = Bot(command_prefix="!", intents=intents)

# Add any tasks/cogs here
bot.add_cog(UserPresenceDetectorCog(bot))


def make_easy_command(
Expand Down Expand Up @@ -252,6 +260,15 @@ async def add_coins(ctx: Context, amount: int) -> None:
await user.async_save()


@bot.command()
async def check_balance(ctx: Context) -> None:
user, _ = await GamUser.objects.async_get_or_create(
discord_id=ctx.message.author.id
)
dm_channel = ctx.author.dm_channel or await ctx.author.create_dm()
await dm_channel.send(f"Your GamCoin balance is {user.gam_coins} coins.")


@bot.command()
@is_in_channel({"bot-commands"})
async def register_score(
Expand Down
40 changes: 40 additions & 0 deletions src/chatbot/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from logging import getLogger
from discord.enums import Status

from django.conf import settings

from discord.ext.commands import Cog, Context
from discord.ext import tasks

from .models import GamUser

from typing import TYPE_CHECKING

logger = getLogger(__name__)


if TYPE_CHECKING:
from discord.ext.commands import Bot


# pylint: disable=inherit-non-class,unsubscriptable-object
class UserPresenceDetectorCog(Cog[Context]):
def __init__(self, bot: "Bot[Context]"):
self.bot = bot
self.check_user_presence.start() # pylint: disable=no-member

@tasks.loop(minutes=1.0)
async def check_user_presence(self) -> None:
for user in self.bot.get_all_members():
gam_user, _ = await GamUser.objects.async_get_or_create(discord_id=user.id)
if (
isinstance(user.status, Status)
and user.status is Status.online
and not user.bot
):
gam_user.gam_coins += settings.GAM_COINS_PER_MINUTE

@check_user_presence.before_loop # type: ignore
async def before_check_user_presence(self) -> None:
logger.info("Waiting for bot to start before checking user presence")
await self.bot.wait_until_ready()
2 changes: 2 additions & 0 deletions src/gam/settings/_gam.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
WAGER_ERROR_REACTION = "❗"
WAGER_NO_MONEY_REACTION = "💸"
WAGER_SUCCESS_REACTION = "✅"

GAM_COINS_PER_MINUTE = 10