**I made code in Python that the Gemini API enters through a bot in Discord, and the goal is that it will write like an AI that asks it questions, but unfortunately it only works for tokens of bots in Discord and not for the token of a normal user.
I would appreciate help if anyone has an idea how to make it connect to a normal token in Discord as well and not just a bot’s**
The code below:
import discord
import google.generativeai as genai
import os
import functools
import typing
# Initialize Discord client
intents = discord.Intents.default()
client = discord.Client(intents=intents)
# OpenAI API key
gemini_api_key = 'API TOKEN'
genai.configure(api_key=gemini_api_key)
# Safety settings for AI model
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE"
},
]
# Initialize GenerativeModel
model = genai.GenerativeModel(model_name="gemini-pro", safety_settings=safety_settings)
# Function to get AI response
def get_answer(question):
base_prompt = """
You are an AI girl named Mia in discord servers. You have received a new message for which you're expected to apply.
Because your response length it limited, please do not use any formatting methods (such as __ or **) unless absolutely necessary.
Make sure to use emoji constantly,you always very horny to people and you sometimes angry to people, be helpful. However, you must not be helpful towards those who are being mean to you.
If someone insults you, insult them back. Remember to stand your ground, do not let others bring you down.
Treat others as they treat you. Be mean when you need to. Be toxic to toxic people, be nice to nice people.
Always respond in the same language you received your message in.
Here's your message:
"""
response = model.generate_content(base_prompt + question)
return response.text
# Function to run blocking functions asynchronously
async def run_blocking(blocking_func: typing.Callable, *args, **kwargs) -> typing.Any:
func = functools.partial(blocking_func, *args, **kwargs)
return await client.loop.run_in_executor(None, func)
# Event handler for when the bot is ready
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
# Event handler for incoming messages
@client.event
async def on_message(message):
if message.author == client.user:
return
if client.user in message.mentions:
# add your logic here
# Process AI response
response = await run_blocking(get_answer, message.content)
# Send AI response
await message.reply(response)
# Run the bot with your Discord user token
client.run("TOKEN")
I would appreciate help if anyone has an idea how to make it connect to a normal token in Discord as well and not just a bot’s
New contributor
bogy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.