Skip to content

Commit 1b5d993

Browse files
committed
Use enums in config, resolve #2821
1 parent 038fc61 commit 1b5d993

File tree

5 files changed

+38
-39
lines changed

5 files changed

+38
-39
lines changed

bot.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from core.clients import ApiClient, PluginDatabaseClient, MongoDBClient
3535
from core.config import ConfigManager
3636
from core.utils import human_join, match_title, normalize_alias
37-
from core.models import PermissionLevel, SafeFormatter, getLogger, configure_logging
37+
from core.models import DMDisabled, PermissionLevel, SafeFormatter, getLogger, configure_logging
3838
from core.thread import ThreadManager
3939
from core.time import human_timedelta
4040

@@ -770,7 +770,7 @@ async def process_dm_modmail(self, message: discord.Message) -> None:
770770
)
771771
return
772772

773-
if self.config["dm_disabled"] >= 1:
773+
if self.config["dm_disabled"] in (DMDisabled.NEW_THREADS, DMDisabled.ALL_THREADS):
774774
embed = discord.Embed(
775775
title=self.config["disabled_new_thread_title"],
776776
color=self.error_color,
@@ -787,7 +787,7 @@ async def process_dm_modmail(self, message: discord.Message) -> None:
787787

788788
thread = await self.threads.create(message.author, message=message)
789789
else:
790-
if self.config["dm_disabled"] == 2:
790+
if self.config["dm_disabled"] == DMDisabled.ALL_THREADS:
791791
embed = discord.Embed(
792792
title=self.config["disabled_current_thread_title"],
793793
color=self.error_color,

cogs/modmail.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from natural.date import duration
1515

1616
from core import checks
17-
from core.models import PermissionLevel, SimilarCategoryConverter, getLogger
17+
from core.models import DMDisabled, PermissionLevel, SimilarCategoryConverter, getLogger
1818
from core.paginator import EmbedPaginatorSession
1919
from core.thread import Thread
2020
from core.time import UserFriendlyTime, human_timedelta
@@ -964,7 +964,7 @@ async def contact(
964964

965965
else:
966966
thread = await self.bot.threads.create(user, creator=ctx.author, category=category)
967-
if self.bot.config["dm_disabled"] >= 1:
967+
if self.bot.config["dm_disabled"] in (DMDisabled.NEW_THREADS, DMDisabled.ALL_THREADS):
968968
logger.info("Contacting user %s when Modmail DM is disabled.", user)
969969

970970
if not silent:
@@ -1450,8 +1450,8 @@ async def enable(self, ctx):
14501450
color=self.bot.main_color,
14511451
)
14521452

1453-
if self.bot.config["dm_disabled"] != 0:
1454-
self.bot.config["dm_disabled"] = 0
1453+
if self.bot.config["dm_disabled"] != DMDisabled.NONE:
1454+
self.bot.config["dm_disabled"] = DMDisabled.NONE
14551455
await self.bot.config.update()
14561456

14571457
return await ctx.send(embed=embed)
@@ -1481,8 +1481,8 @@ async def disable_new(self, ctx):
14811481
description="Modmail will not create any new threads.",
14821482
color=self.bot.main_color,
14831483
)
1484-
if self.bot.config["dm_disabled"] < 1:
1485-
self.bot.config["dm_disabled"] = 1
1484+
if self.bot.config["dm_disabled"] < DMDisabled.NEW_THREADS:
1485+
self.bot.config["dm_disabled"] = DMDisabled.NEW_THREADS
14861486
await self.bot.config.update()
14871487

14881488
return await ctx.send(embed=embed)
@@ -1501,8 +1501,8 @@ async def disable_all(self, ctx):
15011501
color=self.bot.main_color,
15021502
)
15031503

1504-
if self.bot.config["dm_disabled"] != 2:
1505-
self.bot.config["dm_disabled"] = 2
1504+
if self.bot.config["dm_disabled"] != DMDisabled.ALL_THREADS:
1505+
self.bot.config["dm_disabled"] = DMDisabled.ALL_THREADS
15061506
await self.bot.config.update()
15071507

15081508
return await ctx.send(embed=embed)
@@ -1514,13 +1514,13 @@ async def isenable(self, ctx):
15141514
Check if the DM functionalities of Modmail is enabled.
15151515
"""
15161516

1517-
if self.bot.config["dm_disabled"] == 1:
1517+
if self.bot.config["dm_disabled"] == DMDisabled.NEW_THREADS:
15181518
embed = discord.Embed(
15191519
title="New Threads Disabled",
15201520
description="Modmail is not creating new threads.",
15211521
color=self.bot.error_color,
15221522
)
1523-
elif self.bot.config["dm_disabled"] == 2:
1523+
elif self.bot.config["dm_disabled"] == DMDisabled.ALL_THREADS:
15241524
embed = discord.Embed(
15251525
title="All DM Disabled",
15261526
description="Modmail is not accepting any DM messages for new and existing threads.",

core/config.py

+18-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from discord.ext.commands import BadArgument
1313

1414
from core._color_data import ALL_COLORS
15-
from core.models import InvalidConfigError, Default, getLogger
15+
from core.models import DMDisabled, InvalidConfigError, Default, getLogger
1616
from core.time import UserFriendlyTimeSync
1717
from core.utils import strtobool
1818

@@ -98,9 +98,7 @@ class ConfigManager:
9898
"activity_message": "",
9999
"activity_type": None,
100100
"status": None,
101-
# dm_disabled 0 = none, 1 = new threads, 2 = all threads
102-
# TODO: use enum
103-
"dm_disabled": 0,
101+
"dm_disabled": DMDisabled.NONE,
104102
"oauth_whitelist": [],
105103
# moderation
106104
"blocked": {},
@@ -165,7 +163,11 @@ class ConfigManager:
165163
"enable_eval",
166164
}
167165

168-
special_types = {"status", "activity_type"}
166+
enums = {
167+
"dm_disabled": DMDisabled,
168+
"status": discord.Status,
169+
"activity_type": discord.ActivityType
170+
}
169171

170172
defaults = {**public_keys, **private_keys, **protected_keys}
171173
all_keys = set(defaults.keys())
@@ -277,26 +279,15 @@ def get(self, key: str, convert=True) -> typing.Any:
277279
value = strtobool(value)
278280
except ValueError:
279281
value = self.remove(key)
280-
281-
elif key in self.special_types:
282+
283+
elif key in self.enums:
282284
if value is None:
283285
return None
284-
285-
if key == "status":
286-
try:
287-
# noinspection PyArgumentList
288-
value = discord.Status(value)
289-
except ValueError:
290-
logger.warning("Invalid status %s.", value)
291-
value = self.remove(key)
292-
293-
elif key == "activity_type":
294-
try:
295-
# noinspection PyArgumentList
296-
value = discord.ActivityType(value)
297-
except ValueError:
298-
logger.warning("Invalid activity %s.", value)
299-
value = self.remove(key)
286+
try:
287+
value = self.enums[key](value)
288+
except ValueError:
289+
logger.warning("Invalid %s %s.", key, value)
290+
value = self.remove(key)
300291

301292
return value
302293

@@ -355,8 +346,10 @@ def set(self, key: str, item: typing.Any, convert=True) -> None:
355346
except ValueError:
356347
raise InvalidConfigError("Must be a yes/no value.")
357348

358-
# elif key in self.special_types:
359-
# if key == "status":
349+
elif key in self.enums:
350+
if isinstance(item, self.enums[key]):
351+
# value is an enum type
352+
item = item.value
360353

361354
return self.__setitem__(key, item)
362355

core/models.py

+6
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,9 @@ async def publish(self):
257257

258258
async def ack(self):
259259
return
260+
261+
262+
class DMDisabled(IntEnum):
263+
NONE = 0
264+
NEW_THREADS = 1
265+
ALL_THREADS = 2

core/thread.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ async def send(
896896
except Exception as e:
897897
logger.warning("Cannot delete message: %s.", e)
898898

899-
if from_mod and self.bot.config["dm_disabled"] == 2 and destination != self.channel:
899+
if from_mod and self.bot.config["dm_disabled"] == DMDisabled.ALL_THREADS and destination != self.channel:
900900
logger.info("Sending a message to %s when DM disabled is set.", self.recipient)
901901

902902
try:

0 commit comments

Comments
 (0)