I'm trying to code the bot to generate a random percentage when the command !percent is used while also mentioning the user. When I use the command it doesn't seem to work but no errors pop up in the console.
if (message.content == ("!Percent" || message.content == "!percent")) {
if (message.isMemberMentioned()) {
message.isMemberMentioned.users.forEach((k, v) => {
message.channel.send( v + ' is ' + ( Math.floor(Math.random() * 100) + 1 ) + "% ! " );
})
}
}
})
I think you want
if(message.content.toLowerCase() === '!percent'){...
or..
if(['!Percent', '!percent'].includes(message.content)){...
Not
if (message.content == ("!Percent" || message.content == "!percent")) {...
Is wrong as its comparing message content, a string, to a boolean as "!Percent" will always be true.
The Message class doesn't have a #isMemberMentioned method documented (never existed??) if you want support for that edit your post with the function code. Same goes for the isMemberMentioned property on the Message class variable.
Related
I made some bot uptime code but got an error showing 52 years ago The code is below.
const style = 'R'
const starttime = `<t:${Math.floor(client.readyAt / 1000)}` + (style ? `:${style}` : '') + '>'
client.on('messageCreate' , message=>{
if(message.content == "!uptime"){
message.reply(`uptime!\n uptime : ${starttime}`)
}
})
You are setting that outside of the event, where client.readyAt is null. When you divide null by anything except for 0, you get 0. The result would then be <t:0:R>. You could either make this a function, or set it in the event
function generateReadyTimestamp() {
return `<t:${Math.floor(client.readyAt / 1000)}` + (style ? `:${style}` : '') + '>'
}
// ...
message.reply(`uptime!\n uptime : ${generateReadyTimestamp()}`)
Or, setting it inside the callback:
client.on('messageCreate', message => {
if(message.content == "!uptime"){
const starttime = `<t:${Math.floor(client.readyAt / 1000)}` + (style ? `:${style}` : '') + '>'
message.reply(`uptime!\n uptime : ${starttime}`)
}
})
Ive made the command and it works but it doesn't change the volume of the bot it keeps saying "Volume only can be set in a range of `1` - `100`"
but i typed "volume 1 and it didnt work
Command -
} else if (message.content.startsWith('"volume')) {
const args = message.content
if (!message.member.voice.channel) return message.channel.send("I'm sorry, but you need to be in a voice channel to set a volume!");
if (!serverQueue) return message.channel.send("There is nothing playing");
if (!args[1]) return message.channel.send(`The current volume is: **\`${!serverQueue.volume}%\`**`);
if (isNaN(args[1]) || args[1] > 100) return message.channel.send("Volume only can be set in a range of **\`1\`** - **\`100\`**");
serverQueue.volume = args[1];
serverQueue.connection.dispatcher.setVolume(args[1] / 100);
return message.channel.send(`I set the volume to: **\`${args[1]}%\`**`);
return;
Functions -
const dispatcher = serverQueue.connection
.play(ytdl(song.url))
.on("finish", () => {
if (!serverQueue.loop) serverQueue.songs.shift()
play(guild, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Started playing: **${song.title}**`)
Isn't it because you are missing .split() when you are assigning to args?
const args = message.content.split(" ");
It will break the message content by spaces into an array. Your commands would look like this "volume 5.
So, what I want to do is something like this:
User: sends a command like this: mybot!announce #mention (message) (Attachs image)
Bot: gets the mention, message & attachment from the message, and deletes it. Then resends the message in an embed (see picture).
I managed to do everything else but I can't get the attachment & re-send it in the embed. Please, help :p
Here is the code I actually have, in case you need it:
if (command === 'announce')
{
var mention = args[0];
if (!args[0]) return message.channel.send('You need to mention someone!');
var actualMention = null;
if (mention === '#everyone')
{
actualMention = message.mentions.members.every();
}
else
{
actualMention = message.mentions.members.first();
}
var messageAnnouncement = args.slice(1).join(" ");
if (msgAnnouncement === null) return message.channel.send('You need to put the announcement itself!');
var imageAttachments = message.attachments;
const anunouncementEmbed = new Discord.MessageEmbed()
.setAuthor('YPBot - Announcement')
.setTitle(message.author.tag + ' made an announcement')
.setDescription('Mentioning: ' + mention + '\n\n' +
msgAnnouncement)
.setThumbnail(imageAttachments[0]);
message.channel.send(anunouncementEmbed);
}
Hope I explained myself well. In case I didn't, feel free to ask anything.
Message#attachments actually returns a Collection, so if you want the first element you need Collection#first(). From there you can access the MessageAttachment#url property.
.setThumbnail(imageAttachments.first().url);
For my Discord.js bot, I am attempting to create an audit log that sends messages to a specific log channel within every server it is in for user changes. I have a working 'message deleted' audit log function, but when I attempt to carry it over to role adding and deletion, usernames, nicknames, and avatar changes, the bot fails to log this and crashes. How do I fix this issue within my code?
I have included both the message delete audit log message send, and the role add/remove/username change
client.on('messageDelete', function (message) {
if (message.channel.type === 'text') {
// post in the server's log channel, by finding the accuratebotlog channel (SERVER ADMINS **MUST** CREATE THIS CHANNEL ON THEIR OWN, IF THEY WANT A LOG)
var log = message.guild.channels.find('name', CHANNEL)
if (log != null) {
log.sendMessage('**Message Deleted** ' + message.author + '\'s message: ' + message.cleanContent + ' has been deleted.')
}
}
})
// sends message when important (externally editable) user statuses change (for example nickname)
// user in a guild has been updated
client.on('guildMemberUpdate', function (guild, oldMember, newMember) {
// declare changes
var Changes = {
unknown: 0,
addedRole: 1,
removedRole: 2,
username: 3,
nickname: 4,
avatar: 5
}
var change = Changes.unknown
// check if roles were removed
var removedRole = ''
oldMember.roles.every(function (value) {
if (newMember.roles.find('id', value.id) == null) {
change = Changes.removedRole
removedRole = value.name
}
})
// check if roles were added
var addedRole = ''
newMember.roles.every(function (value) {
if (oldMember.roles.find('id', value.id) == null) {
change = Changes.addedRole
addedRole = value.name
}
})
// check if username changed
if (newMember.user.username != oldMember.user.username) {
change = Changes.username
}
// check if nickname changed
if (newMember.nickname != oldMember.nickname) {
change = Changes.nickname
}
// check if avatar changed
if (newMember.user.avatarURL != oldMember.user.avatarURL) {
change = Changes.avatar
}
// post in the guild's log channel
var log = guild.channels.find('name', CHANNEL)
if (log != null) {
switch (change) {
case Changes.unknown:
log.sendMessage('**[User Update]** ' + newMember)
break
case Changes.addedRole:
log.sendMessage('**[User Role Added]** ' + newMember + ': ' + addedRole)
break
case Changes.removedRole:
log.sendMessage('**[User Role Removed]** ' + newMember + ': ' + removedRole)
break
case Changes.username:
log.sendMessage('**[User Username Changed]** ' + newMember + ': Username changed from ' +
oldMember.user.username + '#' + oldMember.user.discriminator + ' to ' +
newMember.user.username + '#' + newMember.user.discriminator)
break
case Changes.nickname:
log.sendMessage('**[User Nickname Changed]** ' + newMember + ': ' +
(oldMember.nickname != null ? 'Changed nickname from ' + oldMember.nickname +
+newMember.nickname : 'Set nickname') + ' to ' +
(newMember.nickname != null ? newMember.nickname + '.' : 'original username.'))
break
case Changes.avatar:
log.sendMessage('**[User Avatar Changed]** ' + newMember)
break
}
}
})
I expected the bot to send a message to my channel saying 'User Role Removed: memberName + their old role', and vise versa for role adding, but my bot fails to send these messages to the bot log channel I had set up.
There is no guild parameter in a GuildMemberUpdate event. Therefore, newMember is undefined because only two parameters are passed.
client.on('guildMemberUpdate', (oldMember, newMember) => {
const guild = newMember.guild;
// continue with code
});
I don't know how to do this and I have been looking for answer but am unable to find it.
if message.content.startswith('^trivia autostart'):
await client.send_message(message.channel, "Game is starting!\n" +
str(player1) + "\n" + str(player2) + "\n" + str(player3) + "\n" +
str(player4) + "\n" + str(player5) + "\n" + str(player6) )
--
I have this code and i'm trying to make it so it when that code gets run that it calls my ^trivia play command without typing it in chat.
Is this possible?
The solution to that would be defining functions for each command you need to be called globally by your bot. Take the following example:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('error' => console.log);
bot.on('message', message => {
let prefix = '!';
let sender = message.author;
let msg = message.content;
let cont = msg.split(' ');
let args = cont.slice(1);
let cmd = msg.startsWith(prefix) ? cont[0].slice(prefix.length).toUpperCase() : undefined;
// Ping function
// can be: function pingCommand () {...}
let pingCommand = () => {
message.channel.send(`Pong!\nTime: ${bot.ping} ms`);
}
// Main command
if (cmd === 'PING') {
pingCommand();
}
// Calling command in another command
if (cmd === 'TEST') {
message.channel.send('Running a ping test on the bot');
pingCommand();
}
});
bot.login(token);
Hope you understand how it would work