Skip to content

Commit

Permalink
channel settings fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Nov 8, 2023
1 parent 866dccf commit c1cb54a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions aethersprite/extensions/base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def get(
channel = ctx.channel # type: ignore
assert channel

val = settings[name].get(ctx, channel=channel)
val = settings[name].get(ctx, channel=channel.id)
default = settings[name].default
await ctx.send(
f":gear: `{name}`\n"
Expand Down Expand Up @@ -104,7 +104,7 @@ async def set(

return

if settings[name].set(ctx, value, channel=channel):
if settings[name].set(ctx, value, channel=channel.id):
await ctx.send(f":thumbsup: Value updated.")
log.info(
f"{ctx.author} updated setting {name}: {value} in {channel}"
Expand Down Expand Up @@ -146,7 +146,7 @@ async def clear(

return

settings[name].set(ctx, None, raw=True, channel=channel)
settings[name].set(ctx, None, raw=True, channel=channel.id)
await ctx.send(":negative_squared_cross_mark: Setting cleared.")
log.info(f"{ctx.author} cleared setting {name} in {channel}")

Expand Down
28 changes: 18 additions & 10 deletions aethersprite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ async def reset(ctx):
from .filters import SettingFilter

# 3rd party
from discord.ext.commands import Context
from sqlitedict import SqliteDict

# local
from . import data_folder
from . import data_folder, log

# TODO cleanup settings for missing servers/channels on startup

settings = {}
"""Setting definitions"""


class Setting(object):

Expand Down Expand Up @@ -90,7 +88,7 @@ def __init__(
self.filter = filter
"""The filter used to manipulate setting input/output"""

def _ctxkey(self, ctx, channel: str | None = None) -> str:
def _ctxkey(self, ctx: Context, channel: int | None = None) -> str:
"""
Get the key to use when storing/accessing the setting.
Expand All @@ -102,21 +100,23 @@ def _ctxkey(self, ctx, channel: str | None = None) -> str:
The composite key
"""

assert ctx.guild
key = str(
ctx.guild["id"] if isinstance(ctx.guild, dict) else ctx.guild.id
)

log.info(f"channel: {self.channel}")
if self.channel:
key += f"#{ctx.channel.id if channel is None else channel}"

return key

def set(
self,
ctx,
value: str,
ctx: Context,
value: str | None,
raw: bool = False,
channel: str | None = None,
channel: int | None = None,
) -> bool:
"""
Change the setting's value.
Expand Down Expand Up @@ -154,10 +154,13 @@ def set(
vals[self.name] = value

self._values[key] = vals
log.info(f"setting: {key} => {vals}")

return True

def get(self, ctx, raw: bool = False) -> str | None:
def get(
self, ctx: Context, raw: bool = False, channel: int | None = None
) -> str | None:
"""
Get the setting's value.
Expand All @@ -170,12 +173,13 @@ def get(self, ctx, raw: bool = False) -> str | None:
The setting's value
"""

key = self._ctxkey(ctx)
key = self._ctxkey(ctx, channel)
val = (
self._values[key][self.name]
if key in self._values and self.name in self._values[key]
else None
)
log.info(f"setting: {key} == {val}")

if not raw and self.filter is not None:
val = self.filter.out(ctx, val)
Expand Down Expand Up @@ -210,3 +214,7 @@ def register(
settings[name] = Setting(
name, default, validator, channel, description, filter=filter
)


settings: dict[str, Setting] = {}
"""Setting definitions"""

0 comments on commit c1cb54a

Please sign in to comment.