Skip to content

Commit e87aa9d

Browse files
AmbratolmAmbratolm
authored andcommitted
Farm: Added farm logging.
1 parent bde6e08 commit e87aa9d

File tree

4 files changed

+165
-18
lines changed

4 files changed

+165
-18
lines changed

bot/cogs/console_cog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async def sync(self, interaction: Interaction, global_sync: bool = True):
7373
await interaction.followup.send(
7474
embed=EmbedX.success(
7575
title="Commands Synchronization",
76-
description=f"{count[0]}/{count[1]} command(s) synchronized.",
76+
description=f"{count[0]}/{count[1]} command(s) synchronized{" globally" if global_sync else f" to guild: {interaction.guild}"}.",
7777
),
7878
ephemeral=True,
7979
)

bot/cogs/game_cogs/farm_cog.py

Lines changed: 129 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import re
22
from random import randint
33

4-
from discord import Member, Message, Role, utils
4+
from discord import (
5+
Attachment,
6+
Embed,
7+
Guild,
8+
HTTPException,
9+
Interaction,
10+
Member,
11+
Message,
12+
Role,
13+
StickerItem,
14+
TextChannel,
15+
User,
16+
VoiceChannel,
17+
app_commands,
18+
utils,
19+
)
520
from discord.ext.commands import Cog
621

722
from bot.main import ActBot
823
from bot.ui.embed import EmbedX
924
from db.actor import Actor
25+
from db.room import Room
1026
from utils.xp import Experience
1127

1228

@@ -17,11 +33,78 @@ class FarmCog(Cog, description="Allow players to gain stats and roles"):
1733
def __init__(self, bot: ActBot):
1834
self.bot = bot
1935

36+
# ----------------------------------------------------------------------------------------------------
37+
# * Set Farm Log
38+
# ----------------------------------------------------------------------------------------------------
39+
@app_commands.guild_only()
40+
@app_commands.checks.has_permissions(administrator=True)
41+
@app_commands.default_permissions(administrator=True)
42+
@app_commands.command(
43+
description="Set or get farm log channel", extras={"category": "Farm"}
44+
)
45+
async def set_farm_log(
46+
self,
47+
interaction: Interaction,
48+
channel: TextChannel | None = None,
49+
unset: bool = False,
50+
):
51+
if not interaction.guild:
52+
return await interaction.response.send_message(
53+
embed=EmbedX.error(
54+
title="Guild Only",
55+
description="This command can only be used in a server.",
56+
),
57+
ephemeral=True,
58+
)
59+
60+
if unset:
61+
self.delete_room(id=FarmCog.__name__, guild=interaction.guild)
62+
return await interaction.response.send_message(
63+
embed=EmbedX.success(
64+
title="Farm Log Channel Unset",
65+
description="Farm log channel has been unset.",
66+
),
67+
ephemeral=True,
68+
)
69+
70+
if not channel:
71+
log_room = self.load_room(id=FarmCog.__name__, guild=interaction.guild)
72+
log_channel = (
73+
interaction.guild.get_channel(log_room.channel_id) if log_room else None
74+
)
75+
return await interaction.response.send_message(
76+
embed=EmbedX.info(
77+
title="Farm Log Channel",
78+
description=(
79+
f"Current farm log channel is {log_channel.mention}"
80+
if log_channel
81+
else "No farm log channel set."
82+
),
83+
),
84+
ephemeral=True,
85+
)
86+
87+
self.save_room(id=FarmCog.__name__, channel=channel)
88+
await interaction.response.send_message(
89+
embed=EmbedX.success(
90+
title="Farm Log Channel Set",
91+
description=f"Farm log channel has been set to {channel.mention}.",
92+
),
93+
ephemeral=True,
94+
)
95+
2096
# ----------------------------------------------------------------------------------------------------
2197
# * On Member Join
2298
# ----------------------------------------------------------------------------------------------------
2399
@Cog.listener()
24100
async def on_member_join(self, member: Member):
101+
log_room = self.load_room(id=FarmCog.__name__, guild=member.guild)
102+
log_channel = (
103+
member.guild.get_channel(log_room.channel_id) if log_room else None
104+
)
105+
if log_channel and isinstance(log_channel, TextChannel):
106+
await log_channel.send(f"🟢 {member.mention} joined.")
107+
25108
db = self.bot.get_db(member.guild)
26109
actor = db.find_one(Actor, Actor.id == member.id)
27110
if not actor:
@@ -34,6 +117,13 @@ async def on_member_join(self, member: Member):
34117
# ----------------------------------------------------------------------------------------------------
35118
@Cog.listener()
36119
async def on_member_remove(self, member: Member):
120+
log_room = self.load_room(id=FarmCog.__name__, guild=member.guild)
121+
log_channel = (
122+
member.guild.get_channel(log_room.channel_id) if log_room else None
123+
)
124+
if log_channel and isinstance(log_channel, TextChannel):
125+
await log_channel.send(f"🔴 {member.mention} left.")
126+
37127
db = self.bot.get_db(member.guild)
38128
actor = db.find_one(Actor, Actor.id == member.id)
39129
if not actor:
@@ -69,21 +159,27 @@ async def on_message(self, message: Message):
69159
print(f"👤 @{member.name} earned {xp_reward} xp.")
70160

71161
# Log xp gain to a "log" named channel
72-
# TODO: This works better with a feature allowing to set log channel per server (maybe a command)
73-
# 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴
74-
# log_channel = utils.find(
75-
# lambda c: "📜・log" in c.name.lower(), message.guild.text_channels
76-
# )
77-
# if log_channel:
78-
# embed = EmbedX.info(
79-
# emoji="", title="", description=f"{member.mention} earned experience."
80-
# )
81-
# embed.add_field(name="Experience 🔼", value=f"**⏫ +{xp_reward} **")
82-
# embed.set_author(
83-
# name=member.display_name, icon_url=member.display_avatar.url
84-
# )
85-
# await log_channel.send(embed=embed)
86-
# 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴
162+
log_room = self.load_room(id=FarmCog.__name__, guild=message.guild)
163+
log_channel = (
164+
message.guild.get_channel(log_room.channel_id) if log_room else None
165+
)
166+
if log_channel and isinstance(log_channel, TextChannel):
167+
# embed = EmbedX.info(
168+
# emoji="",
169+
# title="",
170+
# description=f"⏫ {member.mention} earned experience.",
171+
# )
172+
# embed.add_field(name="Experience 🔼", value=f"**⏫ +{xp_reward} **")
173+
# embed.set_author(
174+
# name=member.display_name, icon_url=member.display_avatar.url
175+
# )
176+
# embed = EmbedX.info(
177+
# emoji="",
178+
# title="",
179+
# description=f"**⏫ +{xp_reward}** XP for {member.mention}.",
180+
# )
181+
# await log_channel.send(embed=embed)
182+
await log_channel.send(f"{member.display_name} earned **{xp_reward}** xp.")
87183

88184
# Try level-up
89185
if actor.try_level_up():
@@ -126,6 +222,23 @@ async def on_message(self, message: Message):
126222

127223
# ----------------------------------------------------------------------------------------------------
128224

225+
def save_room(self, id: str, channel: TextChannel | VoiceChannel):
226+
db = self.bot.get_db(channel.guild)
227+
room = db.find_one(Room, Room.id == id) or self.bot.create_room(id, channel)
228+
room.channel_id = channel.id
229+
room.channel_name = channel.name
230+
room.channel_is_voice = isinstance(channel, VoiceChannel)
231+
db.save(room)
232+
233+
def delete_room(self, id: str, guild: Guild):
234+
db = self.bot.get_db(guild)
235+
room = db.find_one(Room, Room.id == id)
236+
if room:
237+
db.delete(room)
238+
239+
def load_room(self, id: str, guild: Guild) -> Room | None:
240+
return self.bot.get_db(guild).find_one(Room, Room.id == id)
241+
129242
# @staticmethod
130243
# async def try_award_role(member: Member, role_name: str) -> Role | None:
131244
# role = utils.get(member.guild.roles, name=role_name)

bot/main.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22
from typing import Any
33

44
from colorama import Fore
5-
from discord import Guild, Interaction, Member, Message, Object, User, app_commands
5+
from discord import (
6+
Guild,
7+
Interaction,
8+
Member,
9+
Message,
10+
Object,
11+
TextChannel,
12+
User,
13+
VoiceChannel,
14+
app_commands,
15+
)
16+
from discord.abc import Messageable
617
from discord.ext.commands import Bot, Cog
718
from odmantic import SyncEngine, query
819

920
from bot.ui.embed import EmbedX
1021
from db.actor import Actor, DmActor
1122
from db.main import ActDb, DbRef
23+
from db.room import Room
1224
from utils.log import logger
1325
from utils.misc import import_classes, text_block
1426

@@ -209,6 +221,15 @@ def create_dm_actor(self, user: User) -> DmActor:
209221
"""Create dm-actor from given user."""
210222
return DmActor(id=user.id, name=user.name, display_name=user.display_name)
211223

224+
def create_room(self, id: str, channel: TextChannel | VoiceChannel) -> Room:
225+
"""Create Room from given discord channel."""
226+
return Room(
227+
id=id,
228+
channel_id=channel.id,
229+
channel_name=channel.name,
230+
channel_is_voice=isinstance(channel, VoiceChannel),
231+
)
232+
212233
# ----------------------------------------------------------------------------------------------------
213234

214235
async def get_actors_members(

db/room.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from odmantic import Field, Model
2+
3+
4+
# -------------------------------------------------------------------------------------------------
5+
# * Room
6+
# -------------------------------------------------------------------------------------------------
7+
class Room(Model):
8+
model_config = {"collection": "rooms"}
9+
10+
id: str = Field(primary_field=True)
11+
channel_id: int
12+
channel_name: str = ""
13+
channel_is_voice: bool = False

0 commit comments

Comments
 (0)