ReferenceError: message is not defined when trying to put let guild - bots

I was working on my discord bot and I was trying to make it for where guild would just become message.guild and in the process of doing so I got the error let guild = message.guild;
^
ReferenceError: message is not defined
my code is:
client.on('messageReactionAdd', (reaction, user) => {
if(reaction.emoji.name === 'ticket2') {
{
let guild = message.guild;
guild.channels.create(`${message.author.username}s-ticket`, {
type: 'text',
permissionOverwrites: [
{
allow: 'VIEW_CHANNEL',
id: message.author.id
},
{
deny: 'VIEW_CHANNEL',
id: guild.id
},
{
allow: 'VIEW_CHANNEL',
id: '691669966998601788'
}
]
}).then(ch => {
userTickets.set(message.author.id, ch.id);
message.channel.send('Ok! Your ticket will be created shortly');
}).catch(err => console.log(err));
}
}
});

This is pretty simple, there is no message object or variable defined since the callback only takes the reaction of the user (you simply have no message declared before).
I suggest you reading this guide on Discord.js rawEvents

You have never defined the variable message as mentioned. In order to get the guild since you are not in the message event, you can get it though a different property. In your case, to get the guild from which the message that was reacted is from you can do reaction.message.guild.
Reference links:
1. https://discord.js.org/#/docs/main/stable/class/MessageReaction?scrollTo=message
2. https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=guild

Related

duscird.js get the name of the user involved

I have a command in which one user virtually kisses another
The output is a message #User1 kissed #User2
But I want that instead of tags on the user, only names were written, like this so that it was user1 kissed user2
In theory, everything should work like this
`**${message.author.username}** kissed **${userToKiss.username}**`
But if the name for message.author is defined, then there is none for userToKiss and I end up with this
user1 kissed undefined
How can I get the name for the second user?
const { Command } = require('discord.js-commando');
const Discord = require('discord.js');
module.exports = class KissCommand extends Command {
constructor(client) {
super(client, {
name: 'kiss',
memberName: 'kiss',
group: 'reywl',
description: 'Kiss the mentioned user',
guildOnly: true,
args: [
{
key: 'userToKiss',
prompt: 'Please select the member you want to kiss.',
type: 'member',
default: 'isempty',
wait: 0.0001
}
]
});
}
run(message, { userToKiss }) {
if (userToKiss == 'isempty') {
return message.channel.send('Please select the member you want to kiss.')}
if (userToKiss.id == message.author.id) {
return message.channel.send('You cant kiss yourself!');
}
else {
const embed = new Discord.MessageEmbed()
.setDescription(`**${message.author}** kissed **${userToKiss}**`)
message.channel.send(embed)
}
setTimeout(() => message.delete(), 1000)
}};
For deep explanation for your work, to do this you need to code first about the member, we always do about member and author
Author is for the author itself and the member for your members who are you going to call
I don't know if commando and normal command handlers are the same but here's the code what you wanted.
const member = message.mentions.members.first() //this is the way how you ping a member
${member.user.username} //the word user is the user member
for the author you can only do this
${message.author.username}
You can also ping a member using their Id's
const member = message.guild.members.cache.get(args[0])
${member.user.username}
So the main output is
${message.author.username} kissed ${member.user.username}
This is how it works
const kissed = await message.guild.members.fetch(userToKiss.id);
${kissed.user.username}

HangMan Comand Within Command

So what I am trying to do here is when the command is used, the new channel is created, then the user mentions the channel and puts their word, then the hangman is sent within the channel they mentioned. But I get lost cause rn it's running everything at once, how do I make it listen for a new "command"?
const { hangman } = require('reconlx')
module.exports = {
name : 'hangman',
description: "Wanna play hangman?",
aliases: ['hang'],
execute(client, message, args, Discord) {
if(!message.member.hasPermission("MANAGE_MESSAGES")) return message.channel.send('You need manage messages permission.')
const channel = message.mentions.channels.first() || message.guild.channels.cache.get(args[0])
message.guild.channels.create(`HangMan`, {
type: 'text',
permissionOverwrites: [
{
id: message.guild.id,
deny: ['VIEW_CHANNEL']
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ADD_REACTIONS', 'ATTACH_FILES']
}
]
}).then(async channel => {
channel.send("Please mention a channel, then add your word. It should look something like this `#the game channel (your word)`")
message.reply(`Scroll to the very top of the server to add your word! <#${channel.id}>`)
})
if(message.content.includes('#')) {
message.reply('Hangman has started!')};
if(!channel) return message.channel.send('Please specify a channel')
const word = args.slice(1).join(" ")
if(!word) return message.channel.send('Please specify a word to guess.')
const hang = new hangman({
message: message,
word: word,
client: client,
channelID: channel.id,
})
hang.start();
}
}

Discord.js sending an embed to a new channel that has been created and randomly pairing 2 users to battle

How to add multiple embeds to the code below? I am not asking for you to write to code for me I just need some tips and points on how to add embeds. Because I would like to make it so that the new channel is created then you will get 2 or 3 embeds posted to that channel. One embed will have reactions on there and I would like for then the bot to pair 2 random players so they can have a lvl battle again.
if (
guild.channels.cache.find((channel) => channel.name === "t5-battle-channel")
)
return;
if (reaction.emoji.name === "5️⃣") {
let guild = reaction.message.guild;
guild.channels.create("T5 Battle Channel", {
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
{
id: guild.id,
deny: "VIEW_CHANNEL",
},
{
id: "788400016736780338",
allow: ["VIEW_CHANNEL"],
},
],
});
}
if (
guild.channels.cache.find((channel) => channel.name === "t5-battle-channel")
)
return;
if (reaction.emoji.name === "5️⃣") {
let guild = reaction.message.guild;
guild.channels
.create("T5 Battle Channel", {
//Creating the channel
type: "text", //Make sure the channel type is text
permissionOverwrites: [
//Set overwrites
{
id: guild.id,
deny: "VIEW_CHANNEL",
},
{
id: "788400016736780338",
allow: ["VIEW_CHANNEL"],
},
],
})
.then((channel) => {
channel.send(embed).then((embedMessage) => {
embedMessage.react("👍");
embedMessage.react("👎");
});
});
}
let embed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle("⚔️ T5 Battles! ⚔️")
.setDescription(
"Please react with a Thumbs up if you want to be paired with you opponent!"
)
.setTimestamp()
.setFooter("⚔️ 1657 Battles! ⚔️ | ⚔️ Managed by Ukzs⚔️");

Discord bot how to create a private text channel

I'm creating a discord bot using node.js and i want it to create a private text channel on a server and add to it the user sending the command "!create" and the bot itself.
I have found a way to make a text channel using this answer: How to create a text channel
but i can't find a way to make it private and add people to it.
I do it always like this:
const everyoneRole = client.guilds.get('SERVER ID').roles.find('name', '#everyone');
const name = message.author.username;
message.guild.createChannel(name, 'text')
.then(r => {
r.overwritePermissions(message.author.id, { VIEW_CHANNEL: true });
r.overwritePermissions(client.id, { VIEW_CHANNEL: true });
r.overwritePermissions(everyoneRole, { VIEW_CHANNEL: false });
})
.catch(console.error);
First, we define the everyoneRole. Then we use the method overwritePermissions() to overwrite the permissions of the newly created guild textchannel. There we give the message author and the bot the permission to view the channel and we revoke the everyone role the permission to view this channel.
Thanks to #gilles-heinesch for the lead. The API of discord.js got drastically changed over time, so here is an updated version:
const { Client, Permissions } = require('discord.js');
/** #param {string|number} serverId - a "snowflake" ID you can see in address bar */
async function createPrivateChannel(serverId, channelName) {
const guild = await client.guilds.fetch(serverId);
const everyoneRole = guild.roles.everyone;
const channel = await guild.channels.create(channelName, 'text');
await channel.overwritePermissions([
{type: 'member', id: message.author.id, allow: [Permissions.FLAGS.VIEW_CHANNEL]},
{type: 'member', id: client.user.id, allow: [Permissions.FLAGS.VIEW_CHANNEL]},
{type: 'role', id: everyoneRole.id, deny: [Permissions.FLAGS.VIEW_CHANNEL]},
]);
}
https://discord.js.org/#/docs/main/stable/class/ChannelManager
Use cache collection
const channels = message.guild.channels.cache
const myChannel = channels.find(channel => channel.name === 'channel name')

Sending a DM through a command, to a specific person from a database, discord.js-commando

So, I having an issue of sending a DM to a specific person without an author tag, and without a mention of the person. I tried duplicating the mention array:
/*jshint esversion: 6*/
const commando = require('discord.js-commando');
class Msgowner extends commando.Command {
constructor(client) {
super(client, {
name: 'msgowner',
group: 'info',
memberName: 'msgowner',
description: 'Gives rules on mock or legit duels.',
examples: ['ladderrules type'],
});
}
async run(message) {
var user = {
id: '12345',
username: 'Bloodmorphed',
discriminator: '12345',
avatar: 'd510ca3d384a25b55d5ce7f4c259b2d0',
bot: false,
lastMessageID: null,
lastMessage: null,
};
user.send('Test');
}
}
module.exports = Msgowner;
There is a reason why I need to send DMs this way, but I can't seem to figure out how. (The error it gives now is a unknown function). Also replacing id and discriminator with generic numbers, but they are correct in my code.
Try something like this - get the member you're looking for using message.channel.members.find() method:
async run(message) {
// get Collection of members in channel
let members = message.channel.members;
// find specific member in collection - enter user's id in place of '<id number>'
let guildMember = members.find('id', '<id number>');
// send Direct Message to member
guildMember.send('test message');
}
Edit: It looks like it's also possible to find users outside the current channel by doing something like this:
async run(message) {
// get client from message's channel
let client = message.channel.client;
// fetch user via given user id
let user = client.fetchUser('<id number>')
.then(user => {
// once promise returns with user, send user a DM
user.send('Test message');
});
}
Okay, found my answer:
async run(message) {
var user = {
id: '12345,
username: 'Bloodmorphed',
discriminator: '12345',
avatar: 'd510ca3d384a25b55d5ce7f4c259b2d0',
bot: false,
lastMessageID: null,
lastMessage: null,
};
console.log(user);
message.member.user.send('Test');
}
}
module.exports = Msgowner;
EDIT: This was NOT the answer, still looking for one.

Resources