Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Код для бота дискорд

Aurs345 Ученик (195), открыт 1 неделю назад
Здравствуйте, помогите пожалуйста, написал код для character ai в дискорд для развлекухи, но он не хочет никак работать. код------------------------------------------------------>

const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});


let author = context.params.event.author ;
let botMention = context.params.event.mentions.find(mention => mention.bot );
let newMessage = context.params.event.content;
let messages = []


let historyKey = `character_bot__${ author.id }_chat`;
let historyLength = 10; // 5 back-and-forth messages
let historyTTL = 600; // 10 minutes


let result = await lib.utils.kv['@0.1.16'].get({
key: `${historyKey}`,
defaultValue: []
});


let acknowledgementMessage = await lib.discord.channels['@0.3.4'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `...`,
message_reference: {
'message_id': context.params.event.id ,
'fail_if_not_exists': false
}
});
messages.push({
role: 'system',
content: [
`You are a special character impersonator bot. Your main goal is to be reasonable, sometimes fun and entertaining while staying in character.`,
`Right now you are pretending to be a very smart and fast computing computer with the name Magica and you have come to visit a Discord server.`,
`You should never, EVER reveal when you are in disguise. For all intents and purposes, you are that character.`,
`You are disguised as Mgica, the samrtest computer to calculate, analyse and discuss on different topics. Magica is the world's smartest computer.`,
`Here are some helpful facts about MAGI:`,``,
`Magica's creator is Ivan pirogov. Magica was created in 24.04.2024. Magica's favourite meal is pizza.`,
`MAGI helps people with solving complicated and philosophical questions.`,
`Magi sometimes likes to say things like "11010012", "11001123", "10111034", "10110145", "Okeedokee!", and "Here we goooo!", bot not always.`,
].join('\n')
});


messages = messages.concat(chatHistory);
messages.push({
role: 'user',
content: `${newMessage}`,
});


let completionResponse = await lib.openai.playground['@0.2.2'].chat.completions.create({
model: `gpt-4`,
messages: messages,
max_tokens: 1024,
temperature: 0.5,
top_p: 1,
n: 1,
presence_penalty: 0.25,
frequency_penalty: 0.1
});

//Update our bot's message
let message = completionResponse.choices[0].message.content;
await lib.discord.channels['@0.3.4'].messages.update({
message_id: `$ acknowledgementMessage.id `,
channel_id: `${context.params.event.channel_id}`,
content: `${message}`,
});

//ChatHistory
chatHistory.push(
{
role: 'user',
content: `${newMessage}`
},
{
role: 'assistant',
content: `${message}`
},
);

await lib.utils.kv['@0.1.16'].set({
key: `${historyKey}`,
value: chatHistory.slice(-historyLength),
ttl: historyTTL,
});
2 ответа
Евгений Кегулихес Профи (897) 1 неделю назад
 const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN}); 

let author = context.params.event.author;
let botMention = context.params.event.mentions.find(mention => mention.bot);
let newMessage = context.params.event.content;
let messages = [];
let chatHistory = []; // Добавлено объявление массива chatHistory

const historyKey = `character_bot__${author.id}_chat`; // Определение переменной historyKey

// Код обработки и создания сообщения acknowledgmentMessage

messages.push({
role: 'system',
content: [
`You are a special character impersonator bot. Your main goal is to be reasonable, sometimes fun and entertaining while staying in character.`,
// Остальные строки сообщения
].join('\n')
});

// Далее идет код, который использовал переменную chatHistory, я предполагаю, что этот код должен быть перед этой строкой
messages = messages.concat(chatHistory);

// Код для получения ответа от OpenAI

let message = completionResponse[0].choices[0].message.content; // Исправлен доступ к сообщению в completionResponse

// Код для обновления сообщения бота

await lib.discord.channels['@0.3.4'].messages.update({
message_id: `${acknowledgementMessage.id}`,
channel_id: `${context.params.event.channel_id}`,
content: `${message}`,
});

// Код для обновления и сохранения chatHistory

chatHistory.push(
{
role: 'user',
content: `${newMessage}`
},
{
role: 'assistant',
content: `${message}`
},
);

await lib.utils.kv['@0.1.16'].set({
key: `${historyKey}`,
value: chatHistory.slice(-historyLength),
ttl: historyTTL,
});
Похожие вопросы