|
1 | | -# ruff: noqa |
| 1 | +import logging |
2 | 2 |
|
3 | | -# from .basecog import BaseCog |
| 3 | +import openai |
| 4 | +from discord import Embed, Message |
| 5 | +from discord.ext.commands import Bot, Cog |
4 | 6 |
|
5 | | -# class MessageLogger(BaseCog): |
6 | | -# def __init__(): |
| 7 | +from .basecog import BaseCog |
| 8 | + |
| 9 | + |
| 10 | +class CheckMessage(BaseCog): |
| 11 | + """CheckMessage cog with commands for moderation.""" |
| 12 | + |
| 13 | + def __init__(self, bot: Bot) -> None: |
| 14 | + """Initialize the checkmessage cog.""" |
| 15 | + super().__init__(bot) |
| 16 | + self.openai = openai.AsyncOpenAI(base_url="http://localhost:8080", api_key="none") |
| 17 | + self.logger.info("Connected to OpenAI API") |
| 18 | + |
| 19 | + @Cog.listener() |
| 20 | + async def on_message(self, message: Message) -> None: |
| 21 | + """Check the message for spam.""" |
| 22 | + if message.channel.id == 1200508244909621289: |
| 23 | + return |
| 24 | + else: |
| 25 | + logging.info(message.channel.id) |
| 26 | + if message.author.bot: |
| 27 | + return |
| 28 | + if message.guild and message.guild.id != 949183273383395328: |
| 29 | + return |
| 30 | + if len(message.content) <= 1: |
| 31 | + return |
| 32 | + self.logger.info("Checking message %s", message.content) |
| 33 | + history_to_give = "" |
| 34 | + async for messages in message.channel.history(limit=3): |
| 35 | + if messages.id == message.id: |
| 36 | + continue |
| 37 | + history_to_give += f"{messages.author!s}: Start-Of-Message\n{messages.content}\nEnd-Of-Message\n\n" |
| 38 | + response = await self.openai.chat.completions.create( |
| 39 | + model="idk", |
| 40 | + messages=[ |
| 41 | + { |
| 42 | + "role": "system", |
| 43 | + "content": 'You are a moderation bot checking if messages defame gender identities. Please respond with only a "YES", "MAYBE", or "NO" with a short reasoning', |
| 44 | + }, |
| 45 | + { |
| 46 | + "role": "user", |
| 47 | + "content": "context:", |
| 48 | + }, |
| 49 | + { |
| 50 | + "role": "user", |
| 51 | + "content": f"{history_to_give}", |
| 52 | + }, |
| 53 | + { |
| 54 | + "role": "user", |
| 55 | + "content": "message:", |
| 56 | + }, |
| 57 | + { |
| 58 | + "role": "user", |
| 59 | + "content": f"{message.author!s}: Start-Of-Message\n{message.content}\nEnd-Of-Message\n\n", |
| 60 | + }, |
| 61 | + ], |
| 62 | + n=1, |
| 63 | + max_tokens=10, |
| 64 | + ) |
| 65 | + self.logger.info("Response: %s", response.choices[0].message) |
| 66 | + if "YES" in response.choices[0].message.content: |
| 67 | + self.logger.info("Message breaks rules") |
| 68 | + await self.bot.get_channel(1200508244909621289).send( |
| 69 | + embed=Embed( |
| 70 | + title="Message Breaks Rules", |
| 71 | + description=f"Jump To Message: [Here]({message.jump_url}), Reasoning: {response.choices[0].message.content}", |
| 72 | + ), |
| 73 | + ) |
| 74 | + elif "MAYBE" in response.choices[0].message.content: |
| 75 | + self.logger.info("Message possibly breaks rules") |
| 76 | + await self.bot.get_channel(1200508244909621289).send( |
| 77 | + embed=Embed( |
| 78 | + title="Message Possibly Breaks Rules", |
| 79 | + description=f"Jump To Message: [Here]({message.jump_url}), Reasoning: {response.choices[0].message.content}", |
| 80 | + ), |
| 81 | + silent=True, |
| 82 | + ) |
| 83 | + |
| 84 | + @classmethod |
| 85 | + async def setup(cls, bot: Bot) -> None: |
| 86 | + """Initialize the cog and add it to the bot.""" |
| 87 | + logging.getLogger(__name__).info("Initialized %s", cls.__name__) |
| 88 | + await bot.add_cog(cls(bot)) |
| 89 | + |
| 90 | + |
| 91 | +setup = CheckMessage.setup |
0 commit comments