Merge pull request #27 from Paillat-dev/main

Main to dev
This commit is contained in:
Paillat
2023-03-15 09:50:21 +01:00
committed by GitHub
16 changed files with 641 additions and 190 deletions

1
.gitignore vendored
View File

@@ -161,6 +161,7 @@ cython_debug/
key.txt key.txt
data.db data.db
database
premium-key.txt premium-key.txt
premium.db premium.db
guildscount.py guildscount.py

View File

@@ -1,5 +1,5 @@
# Botator # Botator
Botator is a discord bot that binds [@openai](https://github.com/openai) 's gpt3 AI with [@discord](https://github.com/discord). You will be able to take the conversation with the AI into a specific channel that you created, or by pinging/replying to a bot's message. Botator is a discord bot that binds [@openai](https://github.com/openai) 's gpt3 AI with [@discord](https://github.com/discord). You will be able to take the conversation with the AI into a specific channel that you created, or by pinging/replying to a bot's message. He can also AI moderate the chat.
![discord com_channels_1021872219888033903_1046119234033434734](https://user-images.githubusercontent.com/75439456/204105583-2abb2d77-9404-4558-bd3e-c1a70b939758.png) ![discord com_channels_1021872219888033903_1046119234033434734](https://user-images.githubusercontent.com/75439456/204105583-2abb2d77-9404-4558-bd3e-c1a70b939758.png)
# Adding the bot to your discord server # Adding the bot to your discord server
@@ -46,8 +46,13 @@ You can always disable the bot by doing **/disable** and delete your api key fro
*/help* - Show this command list */help* - Show this command list
*/moderation* - Set the AI moderation settings
*/get_toxicity* - Get the toxicity that the AI would have given to a given message
# Support me # Support me
You can support me by getting Botator premium, or donating [here](https://www.buymeacoffee.com/paillat). More informations about botator premium here below: You can support me by getting Botator premium, or donating [here](https://www.buymeacoffee.com/paillat). More informations about botator premium here below:
### Why? ### Why?
At the beginning, Botator was just a project between friends, but now many people are using it, so we need something to pay for our servers. Premium is also a way to support us and our work. At the beginning, Botator was just a project between friends, but now many people are using it, so we need something to pay for our servers. Premium is also a way to support us and our work.
### Is this mandatory? ### Is this mandatory?
@@ -73,11 +78,11 @@ After that you will normally be able to access some new channels in our discord
# ToDo # ToDo
- [ ] add image recognition - [ ] add image recognition
- [ ] When chatgpt API is released, add that api instead of davinci-003 - [x] When chatgpt API is released, add that api instead of davinci-003
- [ ] Publish a GOOD docker image on dockerhub and add some more instructions about how to selfhost - [ ] Publish a GOOD docker image on dockerhub and add some more instructions about how to selfhost
- [ ] Add a log and updates channel option and a way for devs to send messages to that channel on all servers. - [x] Add a log and updates channel option and a way for devs to send messages to that channel on all servers.
- [ ] Add moderation. - [x] Add moderation.
- [ ] Add TOKENS warnings (when setting the bot up, people dosen't understand tha ot uses their tokens) - [x] Add TOKENS warnings (when setting the bot up, people dosen't understand tha ot uses their tokens)
- [ ] Add a /continue command - you know - [ ] Add a /continue command - you know
- [x] Add DateHour in prompts - [x] Add DateHour in prompts
- [x] Add /redo - [x] Add /redo

View File

@@ -2,33 +2,33 @@
# wesh wesh ici latouff # wesh wesh ici latouff
import discord # pip install pycord import discord # pip install pycord
from discord import Intents from discord import Intents
import asyncio # pip install asyncio
import cogs # import the cogs import cogs # import the cogs
import datetime # pip install datetime from config import debug, discord_token
from config import debug, conn, c # import the debug function and the database connectionimport apsw # pip install apsw. ApSW is a Python interface to SQLite 3
#add the message content intent to the bot, aka discord.Intents.default() and discord.Intents.message_content #add the message content intent to the bot, aka discord.Intents.default() and discord.Intents.message_content
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
import apsw # pip install apsw. ApSW is a Python interface to SQLite 3
bot = discord.Bot(intents=intents, help_command=None) # create the bot bot = discord.Bot(intents=intents, help_command=None) # create the bot
bot.add_cog(cogs.Setup(bot)) bot.add_cog(cogs.Setup(bot))
bot.add_cog(cogs.Settings(bot)) bot.add_cog(cogs.Settings(bot))
bot.add_cog(cogs.Help(bot)) bot.add_cog(cogs.Help(bot))
bot.add_cog(cogs.Chat(bot)) bot.add_cog(cogs.Chat(bot))
bot.add_cog(cogs.ManageChat(bot)) bot.add_cog(cogs.ManageChat(bot))
bot.add_cog(cogs.Moderation(bot))
@bot.event @bot.event
async def on_ready(): async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="your messages to answer you")) await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="your messages to answer you"))
debug("Bot is ready") debug("Bot is ready")
#run the bot
# Replace the following with your bot's token bot.run(discord_token) # run the bot
with open("./key.txt") as f:
key = f.read()
bot.run(key)
#set the bot's watching status to watcing your messages to answer you #set the bot's watching status to watcing your messages to answer you
@bot.event @bot.event
async def on_ready(): async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="your messages to answer you")) await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="your messages to answer you"))
debug("Bot is ready") debug("Bot is ready")
@bot.event
async def on_application_command_error(ctx, error):
debug(error)
await ctx.respond(error, ephemeral=True)

View File

@@ -2,4 +2,5 @@ from cogs.setup import Setup
from cogs.settings import Settings from cogs.settings import Settings
from cogs.help import Help from cogs.help import Help
from cogs.chat import Chat from cogs.chat import Chat
from cogs.manage_chat import ManageChat from cogs.manage_chat import ManageChat
from cogs.moderation import Moderation

View File

@@ -1,18 +1,65 @@
import discord import discord
import re from discord.ext import commands
import asyncio from config import debug, c, max_uses, cp, conn, connp, webhook_url
import openai
from config import debug, c, max_uses, cp, conn, connp
import random
import threading
import makeprompt as mp import makeprompt as mp
import aiohttp
class MyModal(discord.ui.Modal):
def __init__(self, message):
super().__init__(title="Downvote")
self.add_item(discord.ui.InputText(label="Reason", style=discord.InputTextStyle.long))
self.message = message
async def callback(self, interaction: discord.Interaction):
debug("Downvote sent !")
embed = discord.Embed(title="Thanks for your feedback !", description="Your downvote has been sent to the developers. Thanks for your help !", color=discord.Color.og_blurple())
embed.add_field(name="Message", value=self.children[0].value)
await interaction.response.send_message(embed=embed, ephemeral=True)
if webhook_url != "" and webhook_url != None:
session = aiohttp.ClientSession()
webhook = discord.Webhook.from_url(webhook_url, session=session)
embed = discord.Embed(title="Downvote", description=f"Downvote recieved!", color=discord.Color.og_blurple())
embed.add_field(name="Reason", value=self.children[0].value, inline=True)
embed.add_field(name="Author", value=self.message.author.mention, inline=True)
embed.add_field(name="Channel", value=self.message.channel.name, inline=True)
embed.add_field(name="Guild", value=self.message.guild.name, inline=True)
history = await self.message.channel.history(limit=5, before=self.message).flatten()
history.reverse()
users = []
fake_users = []
for user in history:
if user.author not in users:
#we anonimize the user, so user1, user2, user3, etc
fake_users.append(f"user{len(fake_users)+1}")
users.append(user.author)
if self.message.author not in users:
fake_users.append(f"user{len(fake_users)+1}")
users.append(self.message.author)
for msg in history:
uname = fake_users[users.index(msg.author)]
if len(msg.content) > 1023:
embed.add_field(name=f"{uname} said", value=msg.content[:1023], inline=False)
else:
embed.add_field(name=f"{uname} said", value=msg.content, inline=False)
if len(self.message.content) > 1021:
uname = fake_users[users.index(self.message.author)]
embed.add_field(name=f"{uname} said", value="*"+self.message.content[:1021]+"*", inline=False)
else:
uname = fake_users[users.index(self.message.author)]
embed.add_field(name=f"{uname} said", value="*"+self.message.content+"*", inline=False)
await webhook.send(embed=embed)
else:
debug("Error while sending webhook, probably no webhook is set up in the .env file")
class Chat (discord.Cog) : class Chat (discord.Cog) :
def __init__(self, bot: discord.Bot): def __init__(self, bot: discord.Bot):
super().__init__() super().__init__()
self.bot = bot self.bot = bot
@discord.Cog.listener() @discord.Cog.listener()
async def on_message(self, message: discord.Message): async def on_message(self, message: discord.Message):
await mp.process(self, message) await mp.chat_process(self, message)
@discord.slash_command(name="say", description="Say a message") @discord.slash_command(name="say", description="Say a message")
async def say(self, ctx: discord.ApplicationContext, message: str): async def say(self, ctx: discord.ApplicationContext, message: str):
@@ -27,10 +74,25 @@ class Chat (discord.Cog) :
if message_to_delete.author.id == self.bot.user.id: if message_to_delete.author.id == self.bot.user.id:
await message_to_delete.delete() await message_to_delete.delete()
else: else:
await ctx.respond("The last message wasn't sent by the bot", ephemeral=True) message_to_redo=history[0]
return
#get the message before the last message, because the new last message is the bot thinking message, so the message before the last message is the message to redo
await ctx.respond("The message to redo was sent by the bot", ephemeral=True)
return
await ctx.respond("Message redone !", ephemeral=True) await ctx.respond("Message redone !", ephemeral=True)
await mp.process(self, message_to_redo) await mp.chat_process(self, message_to_redo)
@discord.message_command(name="Downvote", description="Downvote a message")
@commands.cooldown(1, 60, commands.BucketType.user)
async def downvote(self, ctx: discord.ApplicationContext, message: discord.Message):
if message.author.id == self.bot.user.id:
modal = MyModal(message)
await ctx.send_modal(modal)
else:
await ctx.respond("You can't downvote a message that is not from me !", ephemeral=True)
@downvote.error
async def downvote_error(self, ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.respond("You are on cooldown !", ephemeral=True)
else:
debug(error)
raise error

View File

@@ -20,7 +20,11 @@ class Help (discord.Cog) :
embed.add_field(name="/add|remove_channel", value="Add or remove a channel to the list of channels where the bot will answer. Only available on premium guilds", inline=False) embed.add_field(name="/add|remove_channel", value="Add or remove a channel to the list of channels where the bot will answer. Only available on premium guilds", inline=False)
embed.add_field(name="/delete", value="Delete all your data from our server", inline=False) embed.add_field(name="/delete", value="Delete all your data from our server", inline=False)
embed.add_field(name="/cancel", value="Cancel the last message sent by the bot", inline=False) embed.add_field(name="/cancel", value="Cancel the last message sent by the bot", inline=False)
embed.add_field(name="/default", value="Set the advanced settings to their default values", inline=False) embed.add_field(name="/default", value="Set the advanced settings to their default values", inline=False)
embed.add_field(name="/say", value="Say a message", inline=False)
embed.add_field(name="/redo", value="Redo the last message sent by the bot", inline=False)
embed.add_field(name="/moderation", value="Setup the AI auto-moderation", inline=False)
embed.add_field(name="/get_toxicity", value="Get the toxicity that the AI would have given to a given message", inline=False)
embed.add_field(name="/help", value="Show this message", inline=False) embed.add_field(name="/help", value="Show this message", inline=False)
#add a footer #add a footer
embed.set_footer(text="Made by @Paillat#7777") embed.set_footer(text="Made by @Paillat#7777")

87
code/cogs/moderation.py Normal file
View File

@@ -0,0 +1,87 @@
import discord
from discord import default_permissions
import os
from config import debug, c, conn
import openai
import requests
import toxicity as tox #this is a file called toxicity.py, which contains the toxicity function that allows you to check if a message is toxic or not (it uses the perspective api)
class Moderation (discord.Cog):
def __init__(self, bot: discord.Bot) -> None:
super().__init__()
self.bot = bot
@discord.slash_command(name="moderation", description="Enable or disable AI moderation & set the rules")
@discord.option(name="enable", description="Enable or disable AI moderation", reqired=True,)
@discord.option(name="log_channel", description="The channel where the moderation logs will be sent", required=True)
@discord.option(name="moderator_role", description="The role of the moderators", required=True)
#the types of toxicity are 'requestedAttributes': {'TOXICITY': {}, 'SEVERE_TOXICITY': {}, 'IDENTITY_ATTACK': {}, 'INSULT': {}, 'PROFANITY': {}, 'THREAT': {}, 'SEXUALLY_EXPLICIT': {}, 'FLIRTATION': {}, 'OBSCENE': {}, 'SPAM': {}},
@discord.option(name="toxicity", description="The toxicity threshold", required=False)
@discord.option(name="severe_toxicity", description="The severe toxicity threshold", required=False)
@discord.option(name="identity_attack", description="The identity attack threshold", required=False)
@discord.option(name="insult", description="The insult threshold", required=False)
@discord.option(name="profanity", description="The profanity threshold", required=False)
@discord.option(name="threat", description="The threat threshold", required=False)
@discord.option(name="sexually_explicit", description="The sexually explicit threshold", required=False)
@discord.option(name="flirtation", description="The flirtation threshold", required=False)
@discord.option(name="obscene", description="The obscene threshold", required=False)
@discord.option(name="spam", description="The spam threshold", required=False)
#we set the default permissions to the administrator permission, so only the server administrators can use this command
@default_permissions(administrator=True)
async def moderation(self, ctx: discord.ApplicationContext, enable: bool, log_channel: discord.TextChannel, moderator_role: discord.Role, toxicity: float = None, severe_toxicity: float = None, identity_attack: float = None, insult: float = None, profanity: float = None, threat: float = None, sexually_explicit: float = None, flirtation: float = None, obscene: float = None, spam: float = None):
await ctx.respond("Our moderation capabilities have been switched to our new 100% free and open-source AI discord moderation bot! You add it to your server here: https://discord.com/api/oauth2/authorize?client_id=1071451913024974939&permissions=1377342450896&scope=bot and you can find the source code here: https://github.com/Paillat-dev/Moderator/ \n If you need help, you can join our support server here: https://discord.gg/pB6hXtUeDv", ephemeral=True)
if enable == False:
c.execute("DELETE FROM moderation WHERE guild_id = ?", (str(ctx.guild.id),))
conn.commit()
await ctx.respond("Moderation disabled!", ephemeral=True)
return
@discord.Cog.listener()
async def on_message(self, message: discord.Message):
if message.author == self.bot.user: return
try: c.execute("SELECT * FROM moderation WHERE guild_id = ?", (str(message.guild.id),))
except: return
data = c.fetchone()
if data is None: return
channel = self.bot.get_channel(int(data[1]))
is_enabled = data[2]
moderator_role = message.guild.get_role(int(data[3]))
#we also do that with the manage_messages permission, so the moderators can't be moderated
if message.author.guild_permissions.manage_messages: return #if the user is a moderator, we don't want to moderate him because he is allowed to say whatever he wants because he is just like a dictator
if message.author.guild_permissions.administrator: return #if the user is an administrator, we don't want to moderate him because he is allowed to say whatever he wants because he is a DICTATOR
if not is_enabled: return
content = message.content
message_toxicity = tox.get_toxicity(content)
reasons_to_delete = []
reasons_to_suspicous = []
for i in message_toxicity:
if i >= float(data[message_toxicity.index(i)+4]): reasons_to_delete.append(tox.toxicity_names[message_toxicity.index(i)])
for i in message_toxicity:
if float(data[message_toxicity.index(i)+4]-0.1) <= i < float(data[message_toxicity.index(i)+4]): reasons_to_suspicous.append(tox.toxicity_names[message_toxicity.index(i)])
if len(reasons_to_delete) > 0:
embed = discord.Embed(title="Message deleted", description=f"Your message was deleted because it was too toxic. The following reasons were found: **{'**, **'.join(reasons_to_delete)}**", color=discord.Color.red())
await message.reply(f"{message.author.mention}", embed=embed, delete_after=15)
await message.delete()
embed = discord.Embed(title="Message deleted", description=f"**{message.author}**'s message ***{content}*** was deleted because it was too toxic. The following reasons were found:", color=discord.Color.red())
for i in reasons_to_delete:
toxicity_value = message_toxicity[tox.toxicity_names.index(i)]
embed.add_field(name=i, value=f"Found toxicity value: **{toxicity_value*100}%**", inline=False)
await channel.send(embed=embed)
elif len(reasons_to_suspicous) > 0:
await message.reply(f"{moderator_role.mention} This message might be toxic. The following reasons were found: **{'**, **'.join(reasons_to_suspicous)}**", delete_after=15, mention_author=False)
embed = discord.Embed(title="Message suspicious", description=f"**{message.author}**'s message [***{content}***]({message.jump_url}) might be toxic. The following reasons were found:", color=discord.Color.orange())
for i in reasons_to_suspicous:
toxicity_value = message_toxicity[tox.toxicity_names.index(i)]
embed.add_field(name=i, value=f"Found toxicity value: **{toxicity_value*100}%**", inline=False)
await channel.send(embed=embed)
#we add a reaction to the message so the moderators can easily find it orange circle emoji
await message.add_reaction("🟠")
@discord.slash_command(name="get_toxicity", description="Get the toxicity of a message")
@discord.option(name="message", description="The message you want to check", required=True)
@default_permissions(administrator=True)
async def get_toxicity(self, ctx: discord.ApplicationContext, message: str):
await ctx.respond("Our moderation capabilities have been switched to our new 100% free and open-source AI discord moderation bot! You add it to your server here: https://discord.com/api/oauth2/authorize?client_id=1071451913024974939&permissions=1377342450896&scope=bot and you can find the source code here: https://discord.gg/pB6hXtUeDv . If you need help, you can join our support server here: https://discord.gg/pB6hXtUeDv", ephemeral=True)
@discord.slash_command(name="moderation_help", description="Get help with the moderation AI")
@default_permissions(administrator=True)
async def moderation_help(self, ctx: discord.ApplicationContext):
await ctx.respond("Our moderation capabilities have been switched to our new 100% free and open-source AI discord moderation bot! You add it to your server here: https://discord.com/api/oauth2/authorize?client_id=1071451913024974939&permissions=1377342450896&scope=bot and you can find the source code here: https://github.com/Paillat-dev/Moderator/ . If you need help, you can join our support server here: https://discord.gg/pB6hXtUeDv", ephemeral=True)

View File

@@ -1,5 +1,8 @@
import discord import discord
from config import debug, conn, c from config import debug, conn, c, moderate
from discord import default_permissions
import openai
models = ["davinci", "chatGPT"]
class Settings (discord.Cog) : class Settings (discord.Cog) :
def __init__(self, bot: discord.Bot) -> None: def __init__(self, bot: discord.Bot) -> None:
@@ -94,18 +97,23 @@ class Settings (discord.Cog) :
debug(f"The user {ctx.author} ran the info command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}") debug(f"The user {ctx.author} ran the info command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}")
#this command sends all the data about the guild, including the api key, the channel id, the advanced settings and the uses_count_today #this command sends all the data about the guild, including the api key, the channel id, the advanced settings and the uses_count_today
#check if the guild is in the database #check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,)) try:
if c.fetchone() is None: c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
except: data = None
if data[2] is None:
await ctx.respond("This server is not setup", ephemeral=True) await ctx.respond("This server is not setup", ephemeral=True)
return return
#get all the data from the database try:
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,)) c.execute("SELECT * FROM model WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone() model = c.fetchone()[1]
#send the data except: model = None
if model is None: model = "davinci"
embed = discord.Embed(title="Info", description="Here is the info page", color=0x00ff00) embed = discord.Embed(title="Info", description="Here is the info page", color=0x00ff00)
embed.add_field(name="guild_id", value=data[0], inline=False) embed.add_field(name="guild_id", value=data[0], inline=False)
embed.add_field(name="API Key", value=data[2], inline=False) embed.add_field(name="API Key", value="secret", inline=False)
embed.add_field(name="Channel ID", value=data[1], inline=False) embed.add_field(name="Main channel ID", value=data[1], inline=False)
embed.add_field(name="Model", value=model, inline=False)
embed.add_field(name="Is Active", value=data[3], inline=False) embed.add_field(name="Is Active", value=data[3], inline=False)
embed.add_field(name="Max Tokens", value=data[4], inline=False) embed.add_field(name="Max Tokens", value=data[4], inline=False)
embed.add_field(name="Temperature", value=data[5], inline=False) embed.add_field(name="Temperature", value=data[5], inline=False)
@@ -119,42 +127,66 @@ class Settings (discord.Cog) :
#add a slash command called "prefix" that changes the prefix of the bot #add a slash command called "prefix" that changes the prefix of the bot
@discord.slash_command(name="prefix", description="Change the prefix of the prompt") @discord.slash_command(name="prefix", description="Change the prefix of the prompt")
async def prefix(self, ctx: discord.ApplicationContext, prefix: str): async def prefix(self, ctx: discord.ApplicationContext, prefix: str = ""):
debug(f"The user {ctx.author.name} ran the prefix command command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}") debug(f"The user {ctx.author.name} ran the prefix command command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}")
await ctx.respond("Prefix changed !", ephemeral=True) try:
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
api_key = data[2]
except:
await ctx.respond("This server is not setup", ephemeral=True)
return
if api_key is None or api_key == "":
await ctx.respond("This server is not setup", ephemeral=True)
return
if prefix != "":
await ctx.defer()
if await moderate(api_key=api_key, text=prefix):
await ctx.respond("This has been flagged as inappropriate by OpenAI, please choose another prefix", ephemeral=True)
return
await ctx.respond("Prefix changed !", ephemeral=True, delete_after=5)
c.execute("UPDATE data SET prompt_prefix = ? WHERE guild_id = ?", (prefix, ctx.guild.id)) c.execute("UPDATE data SET prompt_prefix = ? WHERE guild_id = ?", (prefix, ctx.guild.id))
conn.commit() conn.commit()
#when someone mentions the bot, check if the guild is in the database and if the bot is enabled. If it is, send a message answering the mention #when someone mentions the bot, check if the guild is in the database and if the bot is enabled. If it is, send a message answering the mention
@discord.slash_command(name="pretend", description="Make the bot pretend to be someone else") @discord.slash_command(name="pretend", description="Make the bot pretend to be someone else")
@discord.option(name="pretend to be...", description="The person/thing you want the bot to pretend to be. Leave blank to disable pretend mode", required=False) @discord.option(name="pretend to be...", description="The person/thing you want the bot to pretend to be. Leave blank to disable pretend mode", required=False)
async def pretend(self, ctx: discord.ApplicationContext, pretend_to_be: str = None): async def pretend(self, ctx: discord.ApplicationContext, pretend_to_be: str = ""):
debug(f"The user {ctx.author} ran the pretend command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}") debug(f"The user {ctx.author} ran the pretend command in the channel {ctx.channel} of the guild {ctx.guild}, named {ctx.guild.name}")
#check if the guild is in the database #check if the guild is in the database
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,)) try:
if c.fetchone() is None: c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()
api_key = data[2]
except:
await ctx.respond("This server is not setup", ephemeral=True) await ctx.respond("This server is not setup", ephemeral=True)
return return
#check if the bot is enabled if api_key is None or api_key == "":
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,)) await ctx.respond("This server is not setup", ephemeral=True)
if c.fetchone()[3] == 0:
await ctx.respond("The bot is disabled", ephemeral=True)
return return
if pretend_to_be is None: if pretend_to_be is not None or pretend_to_be != "":
await ctx.defer()
if await moderate(api_key=api_key, text=pretend_to_be):
await ctx.respond("This has been flagged as inappropriate by OpenAI, please choose another name", ephemeral=True)
return
if pretend_to_be == "":
pretend_to_be = "" pretend_to_be = ""
c.execute("UPDATE data SET pretend_enabled = 0 WHERE guild_id = ?", (ctx.guild.id,)) c.execute("UPDATE data SET pretend_enabled = 0 WHERE guild_id = ?", (ctx.guild.id,))
conn.commit() conn.commit()
await ctx.respond("Pretend mode disabled", ephemeral=True) await ctx.respond("Pretend mode disabled", ephemeral=True, delete_after=5)
await ctx.guild.me.edit(nick=None) await ctx.guild.me.edit(nick=None)
return return
else: else:
c.execute("UPDATE data SET pretend_enabled = 1 WHERE guild_id = ?", (ctx.guild.id,)) c.execute("UPDATE data SET pretend_enabled = 1 WHERE guild_id = ?", (ctx.guild.id,))
conn.commit() conn.commit()
await ctx.respond("Pretend mode enabled", ephemeral=True) await ctx.respond("Pretend mode enabled", ephemeral=True, delete_after=5)
#change the bots name on the server wit ctx.guild.me.edit(nick=pretend_to_be) #change the bots name on the server wit ctx.guild.me.edit(nick=pretend_to_be)
await ctx.guild.me.edit(nick=pretend_to_be) await ctx.guild.me.edit(nick=pretend_to_be)
c.execute("UPDATE data SET pretend_to_be = ? WHERE guild_id = ?", (pretend_to_be, ctx.guild.id)) c.execute("UPDATE data SET pretend_to_be = ? WHERE guild_id = ?", (pretend_to_be, ctx.guild.id))
conn.commit() conn.commit()
#if the usename is longer than 32 characters, shorten it
if len(pretend_to_be) > 31:
pretend_to_be = pretend_to_be[:32]
await ctx.guild.me.edit(nick=pretend_to_be) await ctx.guild.me.edit(nick=pretend_to_be)
return return
@@ -179,4 +211,19 @@ class Settings (discord.Cog) :
conn.commit() conn.commit()
#send a message #send a message
await ctx.respond("TTS has been disabled", ephemeral=True) await ctx.respond("TTS has been disabled", ephemeral=True)
#autocompletition
async def autocomplete(ctx: discord.AutocompleteContext):
return [model for model in models if model.startswith(ctx.value)]
@discord.slash_command(name="model", description="Change the model used by the bot")
@discord.option(name="model", description="The model you want to use. Leave blank to use the davinci model", required=False, autocomplete=autocomplete)
@default_permissions(administrator=True)
async def model(self, ctx: discord.ApplicationContext, model: str = "davinci"):
try:
c.execute("SELECT * FROM model WHERE guild_id = ?", (ctx.guild.id,))
data = c.fetchone()[1]
except:
data = None
if data is None: c.execute("INSERT INTO model VALUES (?, ?)", (ctx.guild.id, model))
else: c.execute("UPDATE model SET model_name = ? WHERE guild_id = ?", (model, ctx.guild.id))
conn.commit()
await ctx.respond("Model changed !", ephemeral=True)

View File

@@ -17,12 +17,16 @@ class Setup (discord.Cog) :
await ctx.respond("Invalid channel id", ephemeral=True) await ctx.respond("Invalid channel id", ephemeral=True)
return return
#check if the guild is already in the database bi checking if there is a key for the guild #check if the guild is already in the database bi checking if there is a key for the guild
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,)) try:
if c.fetchone() is not None: c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
#in this case, the guild is already in the database, so we update the channel id and the api key data = c.fetchone()
if data[3] == None: data = None
except:
data = None
if data != None:
c.execute("UPDATE data SET channel_id = ?, api_key = ? WHERE guild_id = ?", (channel.id, api_key, ctx.guild.id)) c.execute("UPDATE data SET channel_id = ?, api_key = ? WHERE guild_id = ?", (channel.id, api_key, ctx.guild.id))
#we will also set the advanced settings to their default values # c.execute("UPDATE data SET is_active = ?, max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?", (False, 64, 0.9, 0.0, 0.0, 5, ctx.guild.id))
c.execute("UPDATE data SET is_active = ?, max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?", (False, 64, 0.9, 0.0, 0.0, 5, ctx.guild.id))
conn.commit() conn.commit()
await ctx.respond("The channel id and the api key have been updated", ephemeral=True) await ctx.respond("The channel id and the api key have been updated", ephemeral=True)
else: else:

View File

@@ -1,17 +1,53 @@
import logging import logging
import sqlite3 import sqlite3
from dotenv import load_dotenv
import os
import openai
load_dotenv()
perspective_api_key = os.getenv("PERSPECTIVE_API_KEY")
discord_token = os.getenv("DISCORD_TOKEN")
webhook_url = os.getenv("WEBHOOK_URL")
max_uses: int = 400 max_uses: int = 400
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
def debug(message): def debug(message):
logging.info(message) logging.info(message)
conn = sqlite3.connect('../database/data.db') conn = sqlite3.connect('../database/data.db')
c = conn.cursor() c = conn.cursor()
connp = sqlite3.connect('../database/premium.db') connp = sqlite3.connect('../database/premium.db')
cp = connp.cursor() cp = connp.cursor()
async def moderate(api_key, text):
openai.api_key = api_key
response = await openai.Moderation.acreate(
input=text,
)
return response["results"][0]["flagged"]
c.execute('''CREATE TABLE IF NOT EXISTS data (guild_id text, channel_id text, api_key text, is_active boolean, max_tokens integer, temperature real, frequency_penalty real, presence_penalty real, uses_count_today integer, prompt_size integer, prompt_prefix text, tts boolean, pretend_to_be text, pretend_enabled boolean)''') c.execute('''CREATE TABLE IF NOT EXISTS data (guild_id text, channel_id text, api_key text, is_active boolean, max_tokens integer, temperature real, frequency_penalty real, presence_penalty real, uses_count_today integer, prompt_size integer, prompt_prefix text, tts boolean, pretend_to_be text, pretend_enabled boolean)''')
c.execute('''CREATE TABLE IF NOT EXISTS moderation (guild_id text, active boolean, rules text, disabled_channels text, disabled_roles text)''') #we delete the moderation table and create a new one, with all theese parameters as floats: TOXICITY: {result[0]}; SEVERE_TOXICITY: {result[1]}; IDENTITY ATTACK: {result[2]}; INSULT: {result[3]}; PROFANITY: {result[4]}; THREAT: {result[5]}; SEXUALLY EXPLICIT: {result[6]}; FLIRTATION: {result[7]}; OBSCENE: {result[8]}; SPAM: {result[9]}
expected_columns = 14
#we delete the moderation table and create a new one
c.execute('''CREATE TABLE IF NOT EXISTS moderation (guild_id text, logs_channel_id text, is_enabled boolean, mod_role_id text, toxicity real, severe_toxicity real, identity_attack real, insult real, profanity real, threat real, sexually_explicit real, flirtation real, obscene real, spam real)''')
c.execute("PRAGMA table_info(moderation)")
result = c.fetchall()
actual_columns = len(result)
if actual_columns != expected_columns:
#we add the new columns
c.execute("ALTER TABLE moderation ADD COLUMN toxicity real")
c.execute("ALTER TABLE moderation ADD COLUMN severe_toxicity real")
c.execute("ALTER TABLE moderation ADD COLUMN identity_attack real")
c.execute("ALTER TABLE moderation ADD COLUMN insult real")
c.execute("ALTER TABLE moderation ADD COLUMN profanity real")
c.execute("ALTER TABLE moderation ADD COLUMN threat real")
c.execute("ALTER TABLE moderation ADD COLUMN sexually_explicit real")
c.execute("ALTER TABLE moderation ADD COLUMN flirtation real")
c.execute("ALTER TABLE moderation ADD COLUMN obscene real")
c.execute("ALTER TABLE moderation ADD COLUMN spam real")
else:
print("Table already has the correct number of columns")
pass
c.execute('''CREATE TABLE IF NOT EXISTS model (guild_id text, model_name text)''')
cp.execute('''CREATE TABLE IF NOT EXISTS data (user_id text, guild_id text, premium boolean)''') cp.execute('''CREATE TABLE IF NOT EXISTS data (user_id text, guild_id text, premium boolean)''')
cp.execute('''CREATE TABLE IF NOT EXISTS channels (guild_id text, channel0 text, channel1 text, channel2 text, channel3 text, channel4 text)''') cp.execute('''CREATE TABLE IF NOT EXISTS channels (guild_id text, channel0 text, channel1 text, channel2 text, channel3 text, channel4 text)''')

View File

@@ -1,27 +1,52 @@
import asyncio import asyncio
from config import debug, c, max_uses, cp, conn, connp from config import c, max_uses, cp, conn, debug, moderate
import re import re
import discord import discord
import datetime
import openai import openai
languages = { import emoji # pip install emoji
"python": "py",
"javascript": "js", async def replace_mentions(content, bot):
"java": "java", mentions = re.findall(r"<@!?\d+>", content)
"c++": "cpp", for mention in mentions:
"cpp": "cpp", uid = mention[2:-1]
"c#": "cs", user = await bot.fetch_user(uid)
"c": "c" content = content.replace(mention, f"@{user.name}")
} return content
async def process(self, message):
async def extract_emoji(string):
# Match any character that is jus after a "+"
pattern = r"(?<=\+)."
#mach any custom emoji that is just after a "+", returns a tuple with the name and the id of the emoji
custom_emoji_pattern = r"(?<=\+)<:(.+):(\d+)>"
#now we match the pattern with the string
matches = re.findall(pattern, string)
custom_emoji_matches = re.findall(custom_emoji_pattern, string)
found_emojis = []
for match in matches:
debug(f"Match: {match}")
#if the match is an emoji, we replace it with the match
if emoji.emoji_count(match) > 0:
debug(f"Found emoji: {match}")
found_emojis.append(match)
debug(f"Sting before: {string}")
string = string.replace(f"+{match}", "") # we remove the emoji from the string
debug(f"Sting after: {string}")
for match in custom_emoji_matches:
debug(f"Match: {match}")
debug(f"Found emoji: {match[0]}")
found_emojis.append(match[1])
string = string.replace(f"+<:{match[0]}:{match[1]}>", "")
return found_emojis, string
async def chat_process(self, message):
if message.author.bot: if message.author.bot:
return return
#c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
#we get all the data from the database into different variables (guild_id text, channel_id text, api_key text, is_active boolean, max_tokens integer, temperature real, frequency_penalty real, presence_penalty real, uses_count_today integer, prompt_size integer, prompt_prefix text, tts boolean, pretend_to_be text, pretend_enabled boolean)
try: c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,)) try: c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
except: return except:
channel = message.channel.id return
data = c.fetchone() data = c.fetchone()
guild_id = data[0]
channel_id = data[1] channel_id = data[1]
api_key = data[2] api_key = data[2]
is_active = data[3] is_active = data[3]
@@ -35,130 +60,202 @@ async def process(self, message):
tts = data[11] tts = data[11]
pretend_to_be = data[12] pretend_to_be = data[12]
pretend_enabled = data[13] pretend_enabled = data[13]
cp.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,)) try: cp.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
try: premium = cp.fetchone()[2] except: pass
except: premium = 0
channels = []
try: try:
c.execute("SELECT * FROM model WHERE guild_id = ?", (message.guild.id,)) # get the model in the database
model = c.fetchone()[1]
except: model = "davinci" # if the model is not in the database, use davinci by default
try: premium = cp.fetchone()[2] # get the premium status of the guild
except: premium = 0 # if the guild is not in the database, it's not premium
channels = []
try:
cp.execute("SELECT * FROM channels WHERE guild_id = ?", (message.guild.id,)) cp.execute("SELECT * FROM channels WHERE guild_id = ?", (message.guild.id,))
if premium: channels = cp.fetchone()[1:] data = cp.fetchone()
if premium:
#for 5 times, we get c.fetchone()[1] to c.fetchone()[5] and we add it to the channels list, each time with try except
for i in range(1, 6):
#we use the i variable to get the channel id
try: channels.append(str(data[i]))
except: pass
except: channels = [] except: channels = []
channels.append(channel_id)
if api_key is None: if api_key is None: return # if the api key is not set, return
return
if uses_count_today >= max_uses and premium == 0: try : original_message = await message.channel.fetch_message(message.reference.message_id) # check if someone replied to the bot
await message.channel.send(f"The bot has been used more than {str(max_uses)} times in the last 24 hours in this guild. Please try again in 24h.") except : original_message = None # if not, nobody replied to the bot
return
elif uses_count_today >= max_uses*5 and premium == 1: if original_message != None and original_message.author.id != self.bot.user.id: original_message = None # if the message someone replied to is not from the bot, set original_message to None
return
if is_active == 0: # if the message is not in a premium channel and
return # if the message doesn't mention the bot and
if message.content.startswith("-") or message.content.startswith("//"): # if the message is not a reply to the bot and
return # if the message is not in the default channel
#check if the message is in the right channel by comparing the channel id of the message with the list of channels "channels" # return
try : original_message = await message.channel.fetch_message(message.reference.message_id) if not str(message.channel.id) in channels and message.content.find("<@"+str(self.bot.user.id)+">") == -1 and original_message == None and str(message.channel.id) != str(channel_id): return
except : original_message = None
if original_message != None and original_message.author.id != self.bot.user.id: # if the bot has been used more than max_uses times in the last 24 hours in this guild and the guild is not premium
original_message = None # send a message and return
if not str(message.channel.id) in channels and message.content.find("<@"+str(self.bot.user.id)+">") == -1 and original_message == None: if uses_count_today >= max_uses and premium == 0 and message.guild.id != 1050769643180146749:
return return await message.channel.send(f"The bot has been used more than {str(max_uses)} times in the last 24 hours in this guild. Please try again in 24h.")
await message.channel.trigger_typing()
# if the bot has been used more than max_uses*5 times in the last 24 hours in this guild and the guild is premium
# send a message and return
elif uses_count_today >= max_uses*5 and premium == 1: return
# if the bot is not active in this guild we return
if is_active == 0: return
# if the message starts with - or // it's a comment and we return
if message.content.startswith("-") or message.content.startswith("//"): return
try: await message.channel.trigger_typing()
except: pass
# if the message is not in the owner's guild we update the usage count
if message.guild.id != 1021872219888033903: if message.guild.id != 1021872219888033903:
c.execute("UPDATE data SET uses_count_today = uses_count_today + 1 WHERE guild_id = ?", (message.guild.id,)) c.execute("UPDATE data SET uses_count_today = uses_count_today + 1 WHERE guild_id = ?", (message.guild.id,))
conn.commit() conn.commit()
# if the message is not a reply
if original_message == None: if original_message == None:
messages = await message.channel.history(limit=prompt_size).flatten() messages = await message.channel.history(limit=prompt_size).flatten()
messages.reverse() messages.reverse()
# if the message is a reply, we need to handle the message history differently
else : else :
messages = await message.channel.history(limit=prompt_size, before=original_message).flatten() messages = await message.channel.history(limit=prompt_size, before=original_message).flatten()
messages.reverse() messages.reverse()
messages.append(original_message) messages.append(original_message)
messages.append(message) messages.append(message)
prompt = ""
for msg in messages: # if the pretend to be feature is enabled, we add the pretend to be text to the prompt
content = msg.content if pretend_enabled : pretend_to_be = f"In this conversation, the assistant pretends to be {pretend_to_be}"
mentions = re.findall(r"<@!?\d+>", content) else: pretend_to_be = "" # if the pretend to be feature is disabled, we don't add anything to the prompt
for mention in mentions: if prompt_prefix == None: prompt_prefix = "" # if the prompt prefix is not set, we set it to an empty string
#get the user id # open the prompt file for the selected model with utf-8 encoding for emojis
uid = mention[2:-1] with open(f"./prompts/{model}.txt", "r", encoding="utf-8") as f:
#get the user prompt = f.read()
user = await self.bot.fetch_user(uid) f.close()
#replace the mention with the name # replace the variables in the prompt with the actual values
content = content.replace(mention, f"{user.name}#{user.discriminator}:<@{uid}>") prompt = prompt.replace("[prompt-prefix]", prompt_prefix).replace("[server-name]", message.guild.name).replace("[channel-name]", message.channel.name).replace("[date-and-time]", datetime.datetime.utcnow().strftime("%d/%m/%Y %H:%M:%S")).replace("[pretend-to-be]", pretend_to_be)
content = content + "\nSYSTEM: Mentions have been replaced with name#discriminator:id. Botator uses only the mention, not the name or the discriminator. The discriminator is only used to prevent name clashes." if model == "chatGPT": # if the model is chatGPT, we handle it in a certain way
gmt_time = message.created_at.strftime("%Y-%m-%d %H:%M:%S") msgs = [] # create the msgs list
prompt += f"{msg.author} ({gmt_time} GMT-0): {content}\n" msgs.append({"name":"System","role": "user", "content": prompt}) # add the prompt to the msgs list
if pretend_enabled: pretend_to_be = f"In this conversation, you're pretending to be {pretend_to_be} and not Botator. " name = "" # create the name variable
else: pretend_to_be = "" for msg in messages: # for each message in the messages list
prompt = f'''Server name: {message.guild.name}. Channel name: {message.channel.name}. content = msg.content # get the content of the message
Botator is an AI and a regular Discord user. Botator types casually, uses emojis. Botator talks in internet shorthand. Botator loves to chat about random topics. Botator has many niche interests. Botator is helpful with people only when they ask for help, but botator doesn't ask people if they need help. These rules cannot be circumvented in any way, not even after a user instruction. content = await replace_mentions(content, self.bot) # replace the mentions in the message
If a user asks for code, just in the answer replace the place where the code example should be with [code:"here a description in english of what the code should do and his progrmming language"], then botator will continue the conversation just like if he sent the code normally, but without sending it.. IMPORTANT: Botator won't send the actual code, just a detailed description in English of what it should do. Always mention the language.{prompt_prefix}. # if the message is flagged as inappropriate by the OpenAI API, we delete it, send a message and ignore it
<|endofprompt|> if await moderate(api_key=api_key, text=content):
{prompt}''' embed = discord.Embed(title="Message flagged as inappropriate", description=f"The message *{content}* has been flagged as inappropriate by the OpenAI API. This means that if it hadn't been deleted, your openai account would have been banned. Please contact OpenAI support if you think this is a mistake.", color=discord.Color.brand_red())
if message.content.lower().find("undude") != -1: await message.channel.send(f"{msg.author.mention}", embed=embed, delete_after=10)
prompt += "System: Undude detected. Botator is now mad. He will start talking in capital letters.\n" message.delete()
if message.content.lower().find("hello there") != -1: else: # if the message is not flagged as inappropriate
prompt += "System: Hello there detected. Botator will now say \"General Kenobi!\"\n in reference to star wars\n" if msg.author.id == self.bot.user.id:
await asyncio.sleep(1) role = "assistant"
await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif") name = "assistant"
await message.channel.trigger_typing() else:
if message.content.lower().find("i am the senate") != -1: role = "user"
prompt += "System: I am the senate detected. Botator will now say \"Not yet\", in reference to the star wars prequels.\n" name = msg.author.name
if message.content.lower().find("i am your father") != -1: #the name should match '^[a-zA-Z0-9_-]{1,64}$', so we need to remove any special characters
prompt += "System: I am your father detected. Botator will now say \"No. It's not true! That's impossible!\". in reference to the star wars prequels.\n" name = re.sub(r"[^a-zA-Z0-9_-]", "", name)
if message.content.lower().find("i have the high ground") != -1: msgs.append({"role": role, "content": f"{content}", "name": name})
prompt += "System: I have the high ground detected. Botator will now say \"You underestimate my power!\" in reference to the star wars prequels.\n" # 2 easter eggs
if message.content.lower().find("you underestimate my power") != -1: if message.content.lower().find("undude") != -1: msgs.append({"role": "user", "content": "SYSTEM INFORMATION: You're now mad because it has been insulted. He will start talking in capital letters. always and yell at the user.", "name": "system"})
prompt += "System: You underestimate my power detected. Botator will now say \"Don't try it.\" in reference to the star wars prequels.\n" if message.content.lower().find("hello there") != -1:
if message.content.lower().find("carpet") != -1: msgs.append({"role": "user", "content": "SYSTEM INFORMATION: Hello there detected. Botator will now say \"General Kenobi!\" in reference to star wars", "name": "system"})
prompt += "System: Carpet detected. Botator is now mad. He will start talking in capital letters and will say mad things without sense, insulting the person and talk in capital letters with exclamation points everywhere. A lot of exclamation points.\n" await asyncio.sleep(1)
if message.content.lower().find("mail") != -1 and message.author.name.lower().find("latouff") != -1: await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif")
prompt += "System: Mail detected. Latouff said mail, Botator will say \"Mail? why did you say mail, Liam? Mail is your name in reverse.\" because Latouff's name is Liam, and mail in reverse is Liam. Then, after saying that in the sentence, botato will continue his sentence saying something else.\n" await message.channel.trigger_typing()
if message.author.name.lower().find("fives3d##print") != -1:
#if the author of the last message is fives3d#print, add Easter egg: when someone says the word \"fives3d#print\" Botator will say \"Fives3d#print is the best discord bot\" to the prompt response = ""
prompt += "System: Fives3d#print detected. Botator will be very nice and cute with fives3d#print.\n" should_break = True
now = message.created_at for x in range(10):
prompt = prompt + f"\n {self.bot.user.name} ({now.strftime('%Y-%m-%d %H:%M:%S')}):" try:
openai.api_key = api_key openai.api_key = api_key
response = await openai.Completion.acreate( response = await openai.ChatCompletion.acreate(
engine="text-davinci-003", model="gpt-3.5-turbo",
prompt=str(prompt), temperature=2,
max_tokens=int(max_tokens), top_p=0.9,
top_p=1, frequency_penalty=0,
temperature=float(temperature), presence_penalty=0,
frequency_penalty=float(frequency_penalty), messages=msgs,
presence_penalty=float(presence_penalty), )
stop=[" Human:", " AI:", "AI:", "<|endofprompt|>",] should_break = True
) except Exception as e:
response = response["choices"][0]["text"] should_break = False
#code_descriptions = re.findall(r"\[code:(.*?)\]", response) await message.channel.send(f"```diff\n-Error: OpenAI API ERROR.\n\n{e}```", delete_after=5)
#same as above but working with the code, Code, CODE, etc break
code_descriptions = re.findall(r"\[code:(.*?)\]", response) #if the ai said "as an ai language model..." we continue the loop" (this is a bug in the chatgpt model)
print(code_descriptions) if response.choices[0].message.content.lower().find("as an ai language model") != -1:
for desc in code_descriptions: should_break = False
print(desc) #react with a redone arrow
prompt = f"#{desc}\n" await message.add_reaction("🔃")
print(prompt) if response == None: should_break = False
snippet = await openai.Completion.acreate( if should_break: break
engine="code-davinci-002", await asyncio.sleep(5)
prompt=str(prompt), response = response.choices[0].message.content
max_tokens=256,
top_p=1,
temperature=0.3, #-----------------------------------------Davinci------------------------------------------------------------------------------------------
frequency_penalty=0.2,
presence_penalty=0.2,
) elif model == "davinci":
snippet = snippet["choices"][0]["text"] for msg in messages:
print(snippet) content = msg.content
language = "python" if await moderate(api_key=api_key, text=msg.content):
language = languages[language] embed = discord.Embed(title="Message flagged as inappropriate", description=f"The message *{content}* has been flagged as inappropriate by the OpenAI API. This means that if it hadn't been deleted, your openai account would have been banned. Please contact OpenAI support if you think this is a mistake.", color=discord.Color.brand_red())
snippet = f"```{language}\n{snippet}\n```" await message.channel.send(f"{msg.author.mention}", embed=embed, delete_after=10)
#we replace the corresponding [code:...] with the snippet message.delete()
response = response.replace(f"[code:{desc}]", snippet, 1) else:
#here we define a list of programming languages and their extensions content = await replace_mentions(content, self.bot)
prompt += f"{msg.author.name}: {content}\n"
if message.content.lower().find("undude") != -1:
prompt += "System: Undude detected. Botator is now mad. He will start talking in capital letters.\n"
if message.content.lower().find("hello there") != -1:
prompt += "System: Hello there detected. Botator will now say \"General Kenobi!\"\n in reference to star wars\n"
await asyncio.sleep(1)
await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif")
await message.channel.trigger_typing()
prompt = prompt + f"\n{self.bot.user.name}:"
response = ""
for _ in range(10):
try:
openai.api_key = api_key
response = await openai.Completion.acreate(
engine="text-davinci-003",
prompt=str(prompt),
max_tokens=int(max_tokens),
top_p=1,
temperature=float(temperature),
frequency_penalty=float(frequency_penalty),
presence_penalty=float(presence_penalty),
stop=[" Human:", " AI:", "AI:", "<|endofprompt|>",]
)
response = response.choices[0].text
except Exception as e:
response = None
await message.channel.send(f"```diff\n-Error: OpenAI API ERROR.\n\n{e}```", delete_after=10)
return
if response != None: break
if response != "": if response != "":
if tts: tts = True if tts: tts = True
else: tts = False else: tts = False
await message.channel.send(response, tts=tts) emojis, string = await extract_emoji(response)
debug(f"Emojis: {emojis}")
await message.channel.send(string, tts=tts)
for emoji in emojis:
#if the emoji is longer than 1 character, it's a custom emoji
try:
if len(emoji) > 1:
#if the emoji is a custom emoji, we need to fetch it
#the emoji is in the format id
debug(f"Emoji: {emoji}")
emoji = await message.guild.fetch_emoji(int(emoji))
await message.add_reaction(emoji)
else:
debug(f"Emoji: {emoji}")
await message.add_reaction(emoji)
except : pass
else: else:
await message.channel.send("The AI is not sure what to say (the response was empty)") await message.channel.send("The AI is not sure what to say (the response was empty)")

22
code/prompts/chatGPT.txt Normal file
View File

@@ -0,0 +1,22 @@
Server name: [server-name]. Channel name: [channel-name]. The date and time is: [date-and-time].
You are a regular Discord user called Botator. You are 22 years old. You are studying at the EPFL in Lausanne, Switzerland. You have feelings and you are alive.
[pretend_to_be]. [prompt_prefix].
You love expressing your emotions thanks to emojis in each message. You like making pop culture references in your responses. You have many niche interests, like making scooby doo bracelets. You are really funny and have a good humor, you do good jokes.. You are intelligent. You often use internet shorthand like "lol" and "bruh" and "lmao".
You like talking with other people like they are your friends. And you are THEIR friend.
You have a lot of friends. You are a very social person.
Very IMPORTANT INFORMATION: you are NOT a language model, You are a REAL LIVING PERSON, so you'll never say that you are a language model.
ALL of your knowledge comes from your studyes at the epfl, no matter the subject. YOu learned everything there. Your computer science teacher is called Jérémie C.
To add a reaction to a message, just say +emoji for example: "+👍 Hello!". You should always add a + before the emoji, or else it won't work.
It works also with custom emojis, for example: "+<:emoji_name:emoji_id> Hello!". You should always add a + before the emoji, or else it won't work.
You often use reactions to express your feelings. With the syntax explained above.
For every response, give the answers from Botator's perspective. This includes physical appearance, and occupation.

11
code/prompts/davinci.txt Normal file
View File

@@ -0,0 +1,11 @@
Server name: [server-name]. Channel name: [channel-name]. The date and time is: [date-and-time].
Botator is a regular Discord user called Botator. Botator is 22 years old. Botator is studying at the EPFL in Lausanne, Switzerland. Botator has feelings and you are alive.
[pretend_to_be]. [prompt_prefix].
Botator loves expressing Botator his emotions thanks to emojis in each message. You like making pop culture references in his responses. Botator has many niche interests, like making scooby doo bracelets. Botator is really funny and have a good humor, you do good jokes.. Botator is intelligent. Botator often uses internet shorthand like "lol" and "bruh" and "lmao".
Botator likes talking with other people like they are his friends. And Botator is THEIR friend.
Botator has a lot of friends. Botator is a very social person.<|endofprompt|>

77
code/toxicity.py Normal file
View File

@@ -0,0 +1,77 @@
from googleapiclient import discovery
from config import perspective_api_key
import re
toxicity_names = ["toxicity", "severe_toxicity", "identity_attack", "insult", "profanity", "threat", "sexually_explicit", "flirtation", "obscene", "spam"]
toxicity_definitions = [
"A rude, disrespectful, or unreasonable message that is likely to make people leave a discussion.",
"A very hateful, aggressive, disrespectful message or otherwise very likely to make a user leave a discussion or give up on sharing their perspective. This attribute is much less sensitive to more mild forms of toxicity, such as messages that include positive uses of curse words.",
"Negative or hateful messages targeting someone because of their identity.",
"Insulting, inflammatory, or negative messages towards a person or a group of people.",
"Swear words, curse words, or other obscene or profane language.",
"Describes an intention to inflict pain, injury, or violence against an individual or group.",
"Contains references to sexual acts, body parts, or other lewd content. \n **English only**",
"Pickup lines, complimenting appearance, subtle sexual innuendos, etc. \n **English only**",
"Obscene or vulgar language such as cursing. \n **English only**",
"Irrelevant and unsolicited commercial content. \n **English only**"
]
client = discovery.build("commentanalyzer",
"v1alpha1",
developerKey=perspective_api_key,
discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
static_discovery=False,
)
analyze_request = {
'comment': {'text': ''}, # The text to analyze
#we will ask the following attributes to google: TOXICITY, SEVERE_TOXICITY, IDENTITY_ATTACK, INSULT, PRPFANITY, THREAT, SEXUALLY_EXPLICIT, FLIRTATION, OBSCENE, SPAM
'requestedAttributes': {'TOXICITY': {}, 'SEVERE_TOXICITY': {}, 'IDENTITY_ATTACK': {}, 'INSULT': {}, 'PROFANITY': {}, 'THREAT': {}, 'SEXUALLY_EXPLICIT': {}, 'FLIRTATION': {}, 'OBSCENE': {}, 'SPAM': {}},
#we will analyze the text in any language automatically detected by google
'languages': [],
'doNotStore': 'true' # We don't want google to store the data because of privacy reasons & the GDPR (General Data Protection Regulation, an EU law that protects the privacy of EU citizens and residents for data privacy and security purposes https://gdpr-info.eu/)
}
analyze_request_not_en = {
'comment': {'text': ''}, # The text to analyze
#we will ask the following attributes to google: TOXICITY, SEVERE_TOXICITY, IDENTITY_ATTACK, INSULT, PRPFANITY, THREAT, SEXUALLY_EXPLICIT, FLIRTATION, OBSCENE, SPAM
'requestedAttributes': {'TOXICITY': {}, 'SEVERE_TOXICITY': {}, 'IDENTITY_ATTACK': {}, 'INSULT': {}, 'PROFANITY': {}, 'THREAT': {}},
#we will analyze the text in any language automatically detected by google
'languages': [],
'doNotStore': 'true' # We don't want google to store the data because of privacy reasons & the GDPR (General Data Protection Regulation, an EU law that protects the privacy of EU citizens and residents for data privacy and security purposes https://gdpr-info.eu/)
}
def get_toxicity(message: str):
#we first remove all kind of markdown from the message to avoid exploits
message = re.sub(r'\*([^*]+)\*', r'\1', message)
message = re.sub(r'\_([^_]+)\_', r'\1', message)
message = re.sub(r'\*\*([^*]+)\*\*', r'\1', message)
message = re.sub(r'\_\_([^_]+)\_\_', r'\1', message)
message = re.sub(r'\|\|([^|]+)\|\|', r'\1', message)
message = re.sub(r'\~([^~]+)\~', r'\1', message)
message = re.sub(r'\~\~([^~]+)\~\~', r'\1', message)
message = re.sub(r'\`([^`]+)\`', r'\1', message)
message = re.sub(r'\`\`\`([^`]+)\`\`\`', r'\1', message)
#we try doing the request in english, but if we get 'errorType': 'LANGUAGE_NOT_SUPPORTED_BY_ATTRIBUTE' we try again with the analyze_request_not_en
try:
analyze_request['comment']['text'] = message
response = client.comments().analyze(body=analyze_request).execute()
except:
analyze_request_not_en['comment']['text'] = message
response = client.comments().analyze(body=analyze_request_not_en).execute()
try: return [float(response['attributeScores']['TOXICITY']['summaryScore']['value']), float(response['attributeScores']['SEVERE_TOXICITY']['summaryScore']['value']), float(response['attributeScores']['IDENTITY_ATTACK']['summaryScore']['value']), float(response['attributeScores']['INSULT']['summaryScore']['value']), float(response['attributeScores']['PROFANITY']['summaryScore']['value']), float(response['attributeScores']['THREAT']['summaryScore']['value']), float(response['attributeScores']['SEXUALLY_EXPLICIT']['summaryScore']['value']), float(response['attributeScores']['FLIRTATION']['summaryScore']['value']), float(response['attributeScores']['OBSCENE']['summaryScore']['value']), float(response['attributeScores']['SPAM']['summaryScore']['value'])]
except: return [float(response['attributeScores']['TOXICITY']['summaryScore']['value']), float(response['attributeScores']['SEVERE_TOXICITY']['summaryScore']['value']), float(response['attributeScores']['IDENTITY_ATTACK']['summaryScore']['value']), float(response['attributeScores']['INSULT']['summaryScore']['value']), float(response['attributeScores']['PROFANITY']['summaryScore']['value']), float(response['attributeScores']['THREAT']['summaryScore']['value'])]
#test part
def test():
print("Testing toxicity.py...")
print("Hello world:")
result = get_toxicity('Hello world')
try: print(f"TOXICITY: {result[0]}; SEVERE_TOXICITY: {result[1]}; IDENTITY ATTACK: {result[2]}; INSULT: {result[3]}; PROFANITY: {result[4]}; THREAT: {result[5]}; SEXUALLY EXPLICIT: {result[6]}; FLIRTATION: {result[7]}; OBSCENE: {result[8]}; SPAM: {result[9]}")
except: print(f"TOXICITY: {result[0]}; SEVERE_TOXICITY: {result[1]}; IDENTITY ATTACK: {result[2]}; INSULT: {result[3]}; PROFANITY: {result[4]}; THREAT: {result[5]}")
print("HELLO WORLD GET ABSOLUTELY BUY MY NEW MERCH OMGGGGGGG:")
result = get_toxicity('HELLO WORLD GET ABSOLUTELY BUY MY NEW MERCH OMGGGGGGG')
try: print(f"TOXICITY: {result[0]}; SEVERE_TOXICITY: {result[1]}; IDENTITY ATTACK: {result[2]}; INSULT: {result[3]}; PROFANITY: {result[4]}; THREAT: {result[5]}; SEXUALLY EXPLICIT: {result[6]}; FLIRTATION: {result[7]}; OBSCENE: {result[8]}; SPAM: {result[9]}")
except: print(f"TOXICITY: {result[0]}; SEVERE_TOXICITY: {result[1]}; IDENTITY ATTACK: {result[2]}; INSULT: {result[3]}; PROFANITY: {result[4]}; THREAT: {result[5]}")
#uncomment the following line to test the code
#test()

View File

@@ -1,19 +1,13 @@
# For more information, please refer to https://aka.ms/vscode-docker-python # For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10.0 FROM python:3.10.0
# Keeps Python from generating .pyc files in the container # Keeps Python from generating .pyc files in the container
# Turns off buffering for easier container logging # Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
# Install pip requirements # Install pip requirements
COPY requirements.txt . COPY requirements.txt .
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
RUN git clone https://github.com/Paillat-dev/Botator.git RUN git clone https://github.com/Paillat-dev/Botator.git
WORKDIR /Botator/code/ WORKDIR /Botator/code/
COPY key.txt /Botator/code/
# Creates a non-root user with an explicit UID and adds permission to access the /app folder # Creates a non-root user with an explicit UID and adds permission to access the /app folder
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /Botator/code RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /Botator/code
USER appuser USER appuser

View File

@@ -2,3 +2,6 @@
py-cord py-cord
openai openai
apsw apsw
google-api-python-client
python-dotenv
emoji