📅  最后修改于: 2023-12-03 15:16:05.050000             🧑  作者: Mango
Are you looking to add a fun and interactive feature to your Discord bot? Look no further than the 8 Ball command! With just a few lines of code in JavaScript, you can create a magic 8 ball response within your bot that will provide randomized answers to user's questions.
First, you will need to install the Discord.js library.
npm install discord.js
Once you have Discord.js installed, you can create your 8 ball command by adding the following code snippet:
if (message.content.startsWith('!8ball')) {
const responses = [
'It is certain',
'It is decidely so',
'Without a doubt',
'Yes - definitely',
'You may rely on it',
'As I see it, yes',
'Most likely',
'Outlook good',
'Yes',
'Signs point to yes',
'Reply hazy, try again',
'Better not tell you now',
'Ask again later',
'Cannot predict now',
'Concentrate and ask again',
"Don't count on it",
'Outlook not so good',
'My sources say no',
'Very doubtful'
];
const response = responses[Math.floor(Math.random() * responses.length)];
message.channel.send(`🎱 ${response}`);
}
This code will trigger the command if the message content starts with "!8ball". It will then randomly select a response from the "responses" array and send it to the channel.
To make your 8 ball command even more dynamic, you can use Markdown formatting. For example, you can bold the question text and italicize the response text.
if (message.content.startsWith('!8ball')) {
const responses = [
'It is certain',
'It is decidely so',
'Without a doubt',
'Yes - definitely',
'You may rely on it',
'As I see it, yes',
'Most likely',
'Outlook good',
'Yes',
'Signs point to yes',
'Reply hazy, try again',
'Better not tell you now',
'Ask again later',
'Cannot predict now',
'Concentrate and ask again',
"Don't count on it",
'Outlook not so good',
'My sources say no',
'Very doubtful'
];
const question = message.content.slice(7);
if (!question) {
return message.reply('Please ask a question!');
}
const response = responses[Math.floor(Math.random() * responses.length)];
message.channel.send(`:8ball: **Question:** *${question}*\n:sparkles: **Response:** _${response}_`);
}
This code snippet formats the message with bold and italic text. It also includes a check to make sure the user has provided a question before sending the response.
Adding a fun command like the 8 Ball to your Discord bot is a great way to engage with your users and add some fun interactivity to your server. With just a few lines of JavaScript and some Markdown formatting, you can create a dynamic and enjoyable user experience.