import discord from discord.ext import commands from g4f.client import Client import…
- User
import discord from discord.ext import commands from g4f.client import Client import json import asyncio
class NewAutoMod(commands.Cog): def init(self, bot): self.bot = bot self.client = Client()
async def moderate_text(self, context, text): prompt = f"""Du bist ein Chat-Moderator. Beurteile die folgende Nachricht im Kontext:Kontext: {context}
Zu beurteilende Nachricht: "{text}"
Antworte im folgenden JSON-Format: {{ "status": "SAFE" oder "UNSAFE", "reason": "Grund, falls UNSAFE", "confidence": 0.0 bis 1.0 }} """ try: chat_completion = self.client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}], stream=True ) response = "" for chunk in chat_completion: if chunk.choices[0].delta.content: response += chunk.choices[0].delta.content return json.loads(response) except Exception as e: print(f"Fehler bei der Moderation: {str(e)}") return None
@commands.command() async def moderate(self, ctx, *, text: str): async with ctx.typing(): context = await self.get_context(ctx.channel, 5) result = await self.moderate_text(context, text) if result: status = result['status'] reason = result.get('reason', '') confidence = result.get('confidence', 0.0) embed = discord.Embed(title="Moderationsergebnis", color=discord.Color.green() if status == "SAFE" else discord.Color.red()) embed.add_field(name="Status", value=status, inline=False) embed.add_field(name="Konfidenz", value=f"{confidence:.2f}", inline=False) if reason: embed.add_field(name="Grund", value=reason, inline=False) await ctx.send(embed=embed) else: await ctx.send("Es gab einen Fehler bei der Moderation.") @commands.Cog.listener() async def on_message(self, message): if message.author.bot: return context = await self.get_context(message.channel, 5) result = await self.moderate_text(context, message.content) if result and result['status'] == "UNSAFE": try: await message.delete() embed = discord.Embed(title="Nachricht entfernt", color=discord.Color.red()) embed.add_field(name="Grund", value=result.get('reason', 'Verstoß gegen die Community-Richtlinien'), inline=False) await message.channel.send(f"{message.author.mention}, deine Nachricht wurde entfernt:", embed=embed) except discord.errors.Forbidden: print(f"Keine Berechtigung zum Löschen der Nachricht in {message.channel.id}") async def get_context(self, channel, num_messages): messages = [] async for msg in channel.history(limit=num_messages): messages.append(f"{msg.author.name}: {msg.content}") return "\n".join(reversed(messages))def setup(bot):
Was könnte man hier im Code noch hinzufügen?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.