I am learning how to code Discord bots, and I followed a YouTube video to create a reaction role bot, but I keep getting the error:
DiscordAPIError[50035]: Invalid Form Body name[BASE_TYPE_REQUIRED]: This field is required
I appreciate any help I can get! I copy and pasted the relevant code below.
This is the reactions.js file which is what I think is causing the error:
const {SlashCommandBuilder, EmbedBuilder, PermissionsBitField} = require('discord.js');
const reaction = require('../../models/Reaction');
module.exports = {
data: new SlashCommandBuilder()
.setName('reactionrole')
.setDescription('creates a message with reaction roles')
.addSubcommand(
command => command.setName('add').setDescription('add a reaction role to a message').addStringOption(
option => option.setName('message-id').setDescription('the message to react to').setRequired(true)
)
.addStringOption(option => option.setName('emoji').setDescription('the emoji to react with').setRequired(true)).addRoleOption(
option => option.setName('role').setDescription('the role you want to give').setRequired(true)
)
)
.addSubcommand(
command => command.setName('remove').setDescription('remove a reaction role from a message').addStringOption(
option => option.setName('message-id').setDescription('the message to react to').setRequired(true)
)
.addStringOption(option => option.setName('emoji').setDescription('the emoji to react with').setRequired(true))
),
async execute(interaction) {
const {options, guild, channel} = interaction;
const sub = options.getSubCommand();
const emoji = options.getString('emoji');
let e;
const message = await channel.messages.fetch(options.getString('message-id')).catch(err => {
e = err;
});
if(!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
return await interaction.reply({content: `you don't have perms to use this system`, ephemeral: true});
}
if(e) {
return await interaction.reply({content: `be sure to get a message from ${channel}!`, ephemeral: true});
}
const data = await reaction.findOne({Guild: guild.id, Message: message.id, Emoji: emoji});
switch(sub) {
case 'add':
if(data) {
return await interaction.reply({content: `it looks like you already have this reaction setup using ${emoji} on this message`, ephemeral: true});
} else {
const role = options.getRole('role');
await reaction.create({
// name: a,
Guild: guild.id,
Message: messsage.id,
Emoji: emoji,
Role: role.id,
});
const embed = new EmbedBuilder()
.setColor("Blurple")
.setDescription(`i have added a reaction role to ${message.url} with ${emoji} and the role ${role}`)
await message.react(emoji).catch(err => {});
await interaction.reply({embeds: [embed], ephemeral: true});
}
break;
case 'remove':
if(!data) {
return await interaction.reply({content: `it doesn't look like that reaction role exists`, ephemeral: true});
} else {
await reaction.deleteMany({
Guild: guild.id,
Message: message.id,
Emoji: emoji
});
const embed = new EmbedBuilder()
.setColor("Blurple")
.setDescription(`i have removed the reaction role from ${message.url} with ${emoji}`)
await interaction.reply({embeds: [embed], ephemeral: true});
}
}
},
};
This is the Reaction.js file:
const{model, Schema} = require('mongoose');
let reaction = new Schema({
Guild: String,
Message: String,
Emoji: String,
Role: String,
});
module.exports = model('Reaction', reaction);
This is the index.js file:
require('dotenv').config();
const mongoose = require('mongoose');
const{Client, IntentsBitField, Events} = require('discord.js');
const eventHandler = require('./handlers/eventHandler');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.GuildPresences,
IntentsBitField.Flags.MessageContent,
],
});
(async () => {
try {
mongoose.set('strictQuery', false);
await mongoose.connect(process.env.MONGODB_URI);
console.log("connected to DB");
eventHandler(client);
client.login(process.env.TOKEN);
} catch(error) {
console.log(`Error: ${error}`);
}
})();
const reactions = require('./models/Reaction');
client.on(Events.MessageReactionAdd, async (reaction, user) => {
if(!reaction.message.guildId) return;
if(user.bot) return;
let cID = `<:${reaction.emoji.name}:${reaction.emoji.id}>`;
if(!reaction.emoji.id) cID = reaction.emoji.name;
const data = await reactions.findOne({Guild: reaction.message.guildId, Message: reaction.message.id, Emoji: cID});
if(!data) return;
const guild = await client.guilds.cache.get(reaction.message.guildId);
const member = await guild.members.cache.get(user.Id);
try {
await member.roles.add(data.Role);
} catch(e) {
return;
}
});
client.on(Events.MessageReactionRemove, async (reaction, user) => {
if(!reaction.message.guildId) return;
if(user.bot) return;
let cID = `<:${reaction.emoji.name}:${reaction.emoji.id}>`;
if(!reaction.emoji.id) cID = reaction.emoji.name;
const data = await reactions.findOne({Guild: reaction.message.guildId, Message: reaction.message.id, Emoji: cID});
if(!data) return;
const guild = await client.guilds.cache.get(reaction.message.guildId);
const member = await guild.members.cache.get(user.Id);
try {
await member.roles.remove(data.Role);
} catch(e) {
return;
}
});
I tried searching up the error and using ChatGPT to try and find what's causing the error but nothing worked. I also tried using VSCode's debugging tool, but I am somewhat of a beginner to coding and this is my first time coding in Javascript, so I got very confused...