I'm creating a discord bot to make tickets in a server. I'm using code from a tutorial I found online with a few edits so it works properly. Every time I try to run the code, it says "Uncaught ReferenceError"
ReferenceError: Client is not defined". I've looked for answers on how to fix this, but nothing I've found has worked.
module.exports = {
name: "ticket",
aliases: [],
permissions: [],
description: "open a ticket!",
const: Discord = require('discord.js'),
const: Client = new Client({ intents: [Intents.CHANNEL_CREATE, Intents.CHANNEL_UPDATE, Intents.CHANNEL_DELETE, Intents.CHANNEL_UPDATE, Intents.MESSAGE_CREATE, Intents.MESSAGE_REACTION_ADD, Intents.MESSAGE_REACTION_REMOVE, Intents.MESSAGE_REACTION_REMOVE_ALL, Intents.MESSAGE_REACTION_REMOVE_EMOJI] }),
async execute(message, args, cmd, client, discord) {
const channel = await message.guild.channels.create(`ticket: ${message.author.tag}`);
channel.setParent("1001310144359055401");
channel.updateOverwrite(message.guild.id, {
SEND_MESSAGE: false,
VIEW_CHANNEL: false,
});
channel.updateOverwrite(message.author, {
SEND_MESSAGE: true,
VIEW_CHANNEL: true,
});
const reactionMessage = await channel.send("Thank you for opening a middleman request!");
try {
await reactionMessage.react("🔒");
await reactionMessage.react("â›”");
} catch (err) {
channel.send("Error sending emojis!");
throw err;
}
const collector = reactionMessage.createReactionCollector(
(reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("ADMINISTRATOR"),
{ dispose: true }
);
collector.on("collect", (reaction, user) => {
switch (reaction.emoji.name) {
case "🔒":
channel.updateOverwrite(message.author, { SEND_MESSAGES: false });
break;
case "â›”":
channel.send("Deleting this channel in 5 seconds!");
setTimeout(() => channel.delete(), 5000);
break;
}
});
message.channel
.send(`We will be right with you! ${channel}`)
.then((msg) => {
setTimeout(() => msg.delete(), 7000);
setTimeout(() => message.delete(), 3000);
})
.catch((err) => {
throw err;
});
},
};
Client.login(my_token);
const Discord = require('discord.js')
const Client = new Client({ intents: [Intents.CHANNEL_CREATE, Intents.CHANNEL_UPDATE, Intents.CHANNEL_DELETE, Intents.CHANNEL_UPDATE, Intents.MESSAGE_CREATE, Intents.MESSAGE_REACTION_ADD, Intents.MESSAGE_REACTION_REMOVE, Intents.MESSAGE_REACTION_REMOVE_ALL, Intents.MESSAGE_REACTION_REMOVE_EMOJI] })
This two should not be inside of your execute. It should be outside. And you make it wrong way. To do it.
const Discord = require('discord.js');
const Client = new Client({ intents: [Intents.CHANNEL_CREATE, Intents.CHANNEL_UPDATE, Intents.CHANNEL_DELETE, Intents.CHANNEL_UPDATE, Intents.MESSAGE_CREATE, Intents.MESSAGE_REACTION_ADD, Intents.MESSAGE_REACTION_REMOVE, Intents.MESSAGE_REACTION_REMOVE_ALL, Intents.MESSAGE_REACTION_REMOVE_EMOJI] });
module.exports = {
name: "ticket",
aliases: [],
permissions: [],
description: "open a ticket!",
async execute(message, args, cmd, client, discord) {
//Your codes here
}
EDIT:
And I also noticed that you're using const Discord instead calling the Client, I'm pretty sure that you only misspelled it.
const { Client } = require('discord.js')
Calling the Client, then
const client = new Client({intents: [Intents.CHANNEL_CREATE, Intents.CHANNEL_UPDATE, Intents.CHANNEL_DELETE, Intents.CHANNEL_UPDATE, Intents.MESSAGE_CREATE, Intents.MESSAGE_REACTION_ADD, Intents.MESSAGE_REACTION_REMOVE, Intents.MESSAGE_REACTION_REMOVE_ALL, Intents.MESSAGE_REACTION_REMOVE_EMOJI] })
The way you going to get the intents, const Client must be const client
EDIT:
remove client on:
async execute(message, args, cmd, client, discord)
async execute(message, args, cmd, client, discord) => async execute(message, args, cmd, discord)
Or if you have
const Client = require('discord.js');
const client = new Client({ intents: [Intents.CHANNEL_CREATE, Intents.CHANNEL_UPDATE, Intents.CHANNEL_DELETE, Intents.CHANNEL_UPDATE, Intents.MESSAGE_CREATE, Intents.MESSAGE_REACTION_ADD, Intents.MESSAGE_REACTION_REMOVE, Intents.MESSAGE_REACTION_REMOVE_ALL, Intents.MESSAGE_REACTION_REMOVE_EMOJI] });
on your index.js file, you dont need to call it again to the another file of your commands.
Related
message.content isn't working with any of the commands I made. It doesn't see my message's content. It's only working when I use message.content === ""
My code:
const Discord = require('discord.js');
const { Client, Intents, Collection } = require('discord.js')
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if(message.content === ""){
message.reply("I am working");
}
});
client.login("..");
There is no permission error when using code a, but there is a permission error whenever using code b. Is there a solution?
module.exports = {
name: "NICK",
async execute(message, args, client) {
//A: const member = message.mentions.members.first();
//B: const member = await message.guild.members.cache.get(message.author.id)
console.log(message.author.id)
if (!member) return message.reply("target error");
const arguments = args.shift(1)
if (!arguments) return message.reply("name error");
try {
const arguments = args.shift(1)
member.setNickname(arguments);
}catch (error) {
console.error(error);
}
},
};
I currently have these intents
const { Client, GatewayIntentBits, Collection, MembershipScreeningFieldType, ClientUser, User, time, GuildChannel, GuildManager, MessageManager, GuildMemberManager, GuildBanManager, GuildBan, GuildStickerManager, PermissionsBitField, PermissionOverwriteManager, MessageFlagsBitField, GuildMemberRoleManager, gu } = require('discord.js');
const { record } = require('../config.json');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildBans] });
Checking code B I could say a few things:
You don't need to await a cache.get() call;
If you are the owner of the guild, then the bot needs to have a role above your highest role to change your nickname.
So i need help with the assign roles part - so if someone click on the reaction(emojiID) it should give the user who clicked on it the role(roleID)..
My code below sends the embed in the channel with my title & description and the emoji, but nothing happend if someone clicked on the emoji!
I use SlashCommandBuilder :
const { SlashCommandBuilder } = require('#discordjs/builders');
const fs = require('fs');
const { Client, Intents, MessageEmbed, MessageReaction } = require('discord.js');
const client = new Client({ intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
],
partials: ["MESSAGE", "CHANNEL", "REACTION"]
});
module.exports = {
data: new SlashCommandBuilder()
.setName('reaction')
.setDescription('Create reaction messages')
.addChannelOption((option) => {
return option
.setName('channel')
.setDescription('Choose a channel')
.setRequired(true)
})
...
async execute(interaction, client) {
const channelID = interaction.options.getChannel('channel');
const titleID = interaction.options.getString('title');
const descID = interaction.options.getString('desc');
const emojiID = interaction.options.getString('emoji');
const roleID = interaction.options.getRole('role');
let embed = new MessageEmbed()
.setTitle(titleID)
.setDescription(descID)
channelID.send({ embeds: [embed] }).then((sentMessage) => {
sentMessage.react(emojiID);
The code above is the working part - for the roles - I tried something like
async (reaction, user) => {
if(reaction.interaction.partial) await reaction.interaction.fetch();
if(reaction.partial) await reaction.fetch();
if(user.bot) return;
if(!reaction.interaction.guild) return;
if(reaction.interaction.channel.id === channelID) {
if(reaction.emoji.id === emojiID) {
reaction.interaction.guild.members.cache.get(user.id).roles.add(roleID)
}
}
};
but this is not working, please help :)
const Discord = require("discord.js");
require('dotenv').config();
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('#discordjs/voice');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"], partials: ["CHANNEL"] });
const player = createAudioPlayer();
var channelsToMonitor = ['902193996355485778'];
function joinChannel(channel) {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
return connection;
}
function playAudio(connection) {
// Subscribe the connection to the audio player (will play audio on the voice connection)
const resource = createAudioResource('./music/', 'alarm.mp3');
resource.volume = 1;
player.play(resource);
connection.subscribe(player);
player.on(AudioPlayerStatus.Playing, () => {
console.log('ALRM');
});
}
client.on('ready', () => {
console.log('ready');
})
client.on('messageCreate', async msg => {
try {
if (channelsToMonitor.indexOf(msg.channel.id) !== -1) {
if (msg.content == 'GOGOGO') {
const guild = client.guilds.cache.get("857332849119723520");
const channel = guild.channels.cache.get("921415774676058152");
if (!channel) return console.error("The channel does not exist!");
var connection = joinChannel(channel);
await playAudio(connection);
}
} else {
if (msg.author.bot) return;
}
} catch (err) {
console.error(err.message);
}
});
client.login(process.env.DISCORD_TOKEN_2);
I have set this up from the docs
And I cannot find why no audio is coming out! The bot joins the channel and says it's playing when I use console.log(player) but for some reason, it is deafened and doesn't play any sound.
Bots now join with self deaf by default. Provide the selfDeaf field to stop this:
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: false // this may also be needed
})
How do I make it so that my bot sends a message in a specific channel when a user joins?
When I do this, it logs nothing: (Also here are my bot settings)
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const { MessageEmbed } = require('discord.js');
client.on("ready", () => {
console.log("Cloud Shield has been activated.")
});
client.on("guildMemberAdd", async (member) => {
console.log(member);
});
client.login(process.env.token)
This is because you are not requesting the GUILD_MEMBERS intent.
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS"] });
const { MessageEmbed } = require('discord.js');
client.on("ready", () => {
console.log("Cloud Shield has been activated.")
});
client.on("guildMemberAdd", async (member) => {
console.log(member);
});
client.login(process.env.token)