Skip to content

Commit

Permalink
feat: moderator mode
Browse files Browse the repository at this point in the history
chore: bump version

closes #5
  • Loading branch information
omame committed Dec 17, 2022
1 parent 7680c82 commit bc667a9
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bob/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Constants used by bob.
"""
__version__ = "2.7.4"
__version__ = "2.8.0"

import random

Expand Down
2 changes: 1 addition & 1 deletion cogs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, client: commands.Bot):
for question in questions:
self.question_map.update({question.text + str(question.guild): question})
del questions
self.logger.debug("loaded %d questions.", len(self.question_map.keys()))
self.logger.debug("loaded %d questions.", len(self.question_map))

if os.path.exists("config.json"):
self.logger.debug("loading config...")
Expand Down
2 changes: 0 additions & 2 deletions cogs/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def __init__(self, client: commands.Bot):
@commands.has_permissions(manage_channels=True)
@commands.hybrid_command(brief="Sets the channel that bob will talk in.")
async def channel(self, ctx: commands.Context, channel: discord.TextChannel):
if not ctx.author.resolved_permissions.manage_channels:
return await ctx.reply("You're not allowed to use this command!")
self.logger.debug(f"setting guild {ctx.guild.id}'s channel to {channel.id}")
if str(ctx.guild.id) not in self.config.config["guilds"].keys():
self.config.config["guilds"].update({str(ctx.guild.id): {"channel": channel.id}})
Expand Down
18 changes: 12 additions & 6 deletions cogs/lar.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# LaR: Learn and Reply
import typing

import qna
import discord
import logging
from cogs.modmode import DeleteView, ModMode
from cogs.config import Config
from discord.ext import commands

Expand All @@ -12,8 +11,9 @@ class LaR(commands.Cog):
def __init__(self, client: commands.Bot):
self.client = client
self.logger = logging.getLogger("cogs.LaR")
self.config: typing.Union[Config, None] = client.get_cog("Config")
self.config: Config | None = client.get_cog("Config")
self.logger.debug("registered.")
self.mod_mode: ModMode | None = self.client.get_cog("ModMode")

async def learn(self, message: discord.Message):
if message.author.id in self.config.config["optout"] or message.author.id in self.config.config["blacklist"]:
Expand Down Expand Up @@ -59,15 +59,21 @@ async def reply(self, message: discord.Message):
placeholder = "I don't know what to say (give me some time to learn)"
text = placeholder
server_questions = [q for q in self.config.question_map.values() if q.guild == message.guild.id]
question = None
response = None
if len(server_questions):
question = qna.helpers.get_closest_question(server_questions, content,
message.guild.id)
question = qna.helpers.get_closest_question(server_questions, content)
response = qna.helpers.pick_response(question)
text = response.text or placeholder
if message.content.startswith(self.client.command_prefix):
text += "\n(psst, i don't listen to commands here! if you want to run a command, " \
"go to another channel or use slash commands.)"
await message.reply(text)
view = None
if self.mod_mode.is_in_mod_mode(guild, message.author):
view = DeleteView(self.mod_mode, self.config)
message_reply = await message.reply(text, view=view)
if self.mod_mode.is_in_mod_mode(guild, message.author):
self.mod_mode.save_info(guild, message.author, message_reply, question, response)
self.logger.debug(f"reply: {message.clean_content} -> {text}")

@commands.Cog.listener()
Expand Down
3 changes: 1 addition & 2 deletions cogs/usercommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ async def debug(self, ctx: commands.Context, *, prompt: str):
response = None
server_questions = [q for q in self.config.question_map.values() if q.guild == message.guild.id]
if len(server_questions):
question = qna.helpers.get_closest_question(server_questions, content,
message.guild.id)
question = qna.helpers.get_closest_question(server_questions, content)
response = qna.helpers.pick_response(question)
text = response.text or placeholder
embed = None
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async def on_command_error(ctx: commands.Context, error):
async def on_ready():
cogs = [
"cogs.config",
"cogs.modmode",
"cogs.lar",
"cogs.configuration",
"cogs.optin",
Expand Down
2 changes: 1 addition & 1 deletion qna/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def pick_response(question: Question) -> Response:
return response


def get_closest_question(questions: typing.List[Question], message: str, guild_id: int) -> Question:
def get_closest_question(questions: typing.List[Question], message: str) -> Question:
lowest = None
target = None
for question in questions:
Expand Down

0 comments on commit bc667a9

Please sign in to comment.