我想做一个机器人,这是我的第一个机器人,只是和我的一个伙伴搞砸了。本质上,无论何时他在聊天室中写信,我都希望它回复随机消息。
我看到了很多用于禁令的东西,并且发现了一些可以使用的禁令,但是我似乎无法弄清楚如何在他们按下Enter的任何时候都可以使用该禁令,无论其中的文字如何。
这似乎是我找到的最接近的
const userID = '4608164XXX93150209';
bot.on('message', function(message) {
if (!message.sender === userID) {
if (message.content === 'psst') {
message.channel.send('Hello there!');
}
}
});
任何帮助将不胜感激。
请清楚一点,我希望他们在聊天中说出任何话,不需要前缀,并让漫游器以预定义列表中的随机内容作为答复,但仅针对该用户。
答案 0 :(得分:0)
无论我从您的问题中了解到什么,下面的代码都更具可读性:
const blocked_users = ['4608164XXX93150209']; //you may have one more friend like him
bot.on('message', function(message) {
if (blocked_users.indexOf(message.sender) === -1) {
if (message.content === 'psst') {
message.channel.send('Hello there!');
}
} else {
message.channel.send('Get Away from this channel!');
}
});
答案 1 :(得分:0)
假设您使用的是discord.js
,看起来像是一样,则需要使用message.author
属性。
const specificUsers = ['ID Here', 'ID #2', 'ID #3', 'etc.']; // list of ids to detect
const messages = ['Message 1', 'Message 2', 'Message 3']; // list of messages to pick random from
// message event
bot.on('message', (message) => {
// if the author's id matches one of the blacklisted ids
if (specificUsers.includes(message.author.id))
// send random message from array
return message.channel.send(
messages[Math.floor(Math.random() * messages.length)]
);
});
const specificUsers = ['123456789', '987654321']; // list of ids to detect
const messages = ['hello', 'goodbye']; // list of messages to pick random from
// example message event
const message = {
content: 'hello',
author: {
id: '123456789'
}
}
// if the author's id matches one of the blacklisted ids
if (specificUsers.includes(message.author.id))
// send random message from array
console.log(messages[Math.floor(Math.random() * messages.length)]);
答案 2 :(得分:-1)
这是我用来从输入字符串生成随机消息的一些代码。它依靠同一目录中的文本文件textcorpus.txt起作用。您可以使用互联网上的短篇小说来填充此文件。
以运行功能为例来生成随机对话。由于这是打字稿,您可能希望javascript只删除所有部分,所以javascript linter不满意,它应该可以工作。
run();
async function run() {
console.log(`START CONVERSATION, LENGTH: ${100} CHUNKS`);
let conversationLength = 100;
function togglePartner(partner: string) {
if (partner == 'Anton') return 'Bella';
if (partner == 'Bella') return 'Anton';
else return 'Anton';
}
const sendMessage = console.log;
let i = 0;
let lastMessage = '';
let partner = 'Anton';
while (i < conversationLength) {
let res = await getResponse(lastMessage);
res.forEach((sen) => {
sendMessage(`${partner}: ${sen}`);
lastMessage = sen;
});
partner = togglePartner(partner);
i++;
}
}
function roll(zto: number) {
return Math.random() < zto;
}
export function randomBetween(x: number, y: number) {
return x + Math.floor(Math.random() * (y - x));
}
export function randomChoice(choices: Array<any>) {
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}
export async function getResponse(input: string): Promise<Array<string>> {
// only affirmation + dryup:
let finalArray = [];
if (input[input.length - 1] == '?') finalArray.push(affirmation());
else if (roll(0.4)) {
// frage zurück stellen.
finalArray.push(question(input));
} else {
let file = await promises.readFile('./textcorpus.txt');
let corpus = file.toString();
let areaIndexStart = randomBetween(0, corpus.length - 300);
let area = corpus.substring(areaIndexStart, areaIndexStart + 300);
area = area.replace(/[\s]/g, ' ');
area = area.replace(/["']/g, '');
let sentences = area.split(/[.!?]/).map((s) => s.trim());
sentences.pop();
sentences.shift();
if (sentences.length < 1) {
finalArray.push(affirmation());
} else {
// determine count of sentences to return:
let senNum = randomBetween(1, Math.min(sentences.length, 5));
for (let i = 0; i < senNum; i++) {
if (sentences[i].length > 1000) {
// split sentence up and add as multiple:
sentences[i]
.split(',')
.map((p) => p.trim())
.forEach((p) => finalArray.push(p));
} else {
finalArray.push(sentences[i]);
}
}
/*
if (roll(0.1)) finalArray.push(affirmation());
*/
}
}
finalArray = finalArray.map((e, i) =>
e[e.length - 1] != '?' && i == finalArray.length - 1
? addEndSign(e.trim())
: e
);
return finalArray;
}
function addEndSign(input: string) {
return (
input +
randomChoice([
'!!',
'.',
'!',
'?',
'.',
'.',
'!?',
'??',
'...',
'.',
'.',
'.',
'.',
])
);
}
function question(input: string) {
if (roll(0.65) && input.length > 10) {
let numberOfWordsForQuestion = randomBetween(1, 5);
return (
input
.split(/[.!?\s]/)
.filter((e, i) => i < numberOfWordsForQuestion)
.join(' ') + '?'
);
} else {
// question related to input
// generic question
return affirmation();
}
}
function affirmation() {
return randomChoice([
'yeah',
'yea',
'no',
'just kidding',
'not really',
'yes wtf',
'youre right',
'no, actually not',
'no',
'naah',
'yessir',
'hahaha yes',
'absolutely',
'nah',
'uhm yes',
'I think so',
"surprising, isn't it?",
"You don't think so?",
'omg, really?',
'really?',
'Thats not good',
'I like that',
'hmmm',
'weird flex bro',
'This is not true',
'you sure?',
'Why?',
'No way!',
'Why that?',
'where though?',
'so what?',
'sure?',
'wtf?',
'Hav you seen that?',
'right now?',
'what do you think?',
'wdym?',
'lol',
'almost certainly',
'no way!?',
'me bored, can you change the topic?',
'okay',
'lets talk about something else',
'Can you say that again?',
'Wait what?',
'eehm what?',
'interesting',
'cant believe that',
'yeah yeah yeah',
'ehem',
'uhmm what?',
'not really',
'',
]);
}