With Discord.io I'm trying to send a message to a different channel than what it is sent from, but after a lot of Googling and experimenting I still can't seem to find a way to do so.
I tried to simply change the channelID to the channel I want it to send to and overide the channelID to the channel I want it to send to, but with no success
For example:
bot.on('message', function (user, userID, channelID, message, evt) {
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0].toLowerCase();
args = args.splice(1);
switch(cmd) {
case 'test':
bot.sendMessage({
to: // channel id here,
message: 'If all went well this got sent in another channel'
});
break;
default:
bot.sendMessage({
to: channelID,
message: 'I wasn\'t able to understand this command, please try again...'
});
break;
}
}
});
Hopefully you can help me out.
If there is something not clear for you, feel free to ask.
Thanks in advance.
So I found out why it didn't work, in the documentation it says channelID is Snowflake. What I didn't know is that this looks a lot like just a regular string. So when I tried to overide channelID by a string, it seemed to work. Like this:
bot.on('message', function (user, userID, channelID, message, evt) {
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0].toLowerCase();
args = args.splice(1);
switch(cmd) {
case 'test':
bot.sendMessage({
to: 'your channel id here',
message: 'If all went well this got sent in another channel'
});
break;
default:
bot.sendMessage({
to: channelID,
message: 'I wasn\'t able to understand this command, please try again...'
});
break;
}
}
});
Related
i learn a little with the java script and discord.js and i have a little problem
I'am making a fun command if i mention someone and the message it's create a webhook with his username + avatar and send the message i want
The code work but after 10 webhooks created i can't continue use the commands, is it possible to delete the webhook after using it or just delete all webhook on the channel ?
I'm using node and i have install hookcord for send message with webhooks
my code:
if (message.content.startsWith(prefix + "say")) //say something like if it's a member
{
message.delete()
args[0] = message.mentions.members.first()
var usermentions = args[0]
let msg = args[1];
message.channel.createWebhook(usermentions.displayName, usermentions.user.displayAvatarURL).then(wb =>
{
var hookcord = require('hookcord');
var Hook = new hookcord.Hook()
.setLink(`https://discordapp.com/api/webhooks/${wb.id}/${wb.token}`)
.setPayload({
'title': usermentions.displayName,
'avatar': usermentions.user.displayAvatarURL,
'content': msg
})
.fire()
.then(function(response) {})
.catch(function(e) {})
})
}
Thanks in advance for your times and your help !
Yes, you can use channel.fetchWebhooks() and then use a forEach loop to delete all of them:
if(message.content === 'delete-webbooks'){
message.channel.fetchWebhooks().then((webhooks) => {
webhooks.forEach((wh) => wh.delete());
});
}
You can also replace:
.then(function(response) {})
by:
.then(function(response) {
wb.delete();
})
I think it's the best solution.
Now, before you say that this has been posted before, I have a different situation.
With that out of the way, let's get on with the question.
I am making a Discord bot for a friend that does duties for the group and things like that.
Quick note too, I am using the Sitepoint version of Discord.JS because I'm a beginner.
I want the bot to send a message to a certain channel when the show gets canceled for a reason. For example, they would send something like this:
afv!cancel Roblox went down.
or something similar.
But every time it sends a message, every space turns into a comma like this:
:x: The show has been cancelled because: "Roblox,went,down.". Sorry for that!
Here's the index.js code that handles executing commands:
bot.on('message', msg => {
const args = msg.content.split(/ +/);
const command = args.shift().toLowerCase();
const prefix = command.startsWith("afv!");
if (prefix == true) {
console.info(`Called command: ${command}`);
if (!bot.commands.has(command)) return;
msg.delete(1);
try {
bot.commands.get(command).execute(msg, args, bot);
} catch (error) {
console.error(error);
msg.reply('there was an error trying to execute that command!');
};
And the cancelled.js file:
module.exports = {
name: 'afv!cancel',
description: "in-case the show gets cancelled",
execute(msg, args, bot) {
if (msg.member.roles.find(r => r.name === "Bot Perms")) {
const reason = args.replace(/,/g, " ");
bot.channels.get('696135370987012240').send(':x: **The show has been cancelled** because: "' + args + '". *Sorry for that!*');
bot.user.setActivity("AFV! | afv!help", { type: 'PLAYING' });
} else {
msg.reply('you are missing the role: Bot Perms!');
}
},
};
By the way, upon executing the command, it prints this:
TypeError: args.replace is not a function
Thanks for reading! :)
From what I can see, here
const reason = args.replace(/,/g, " ");
bot.channels.get('696135370987012240').send(':x: **The show has been cancelled** because: "' + args + '". *Sorry for that!*');
you are making a const reason, wherein you try to handle that as a string and replace all commas with spaces. Unfortunately, even though it can be displayed in a string and looks to be one, in reality it is an array, so replace() won't work on it. To be able to use replace() you need to first transform your array to an actual string:
const reason = args.join().replace(/,/g, " ");
With that done you won't be seeing this pesky "not a function" error, and you can easily use reason to display your message:
bot.channels.get('696135370987012240').send(':x: **The show has been cancelled** because: "' + reason + '". *Sorry for that!*');
I have never coded a Discord Bot before but am familiar with Javascript so I figured I would give it a shot. I used the beginner files from this site Digital Trends but am running into some issues.
I have the bot running in my server and the basic command swapped from "!" to "?" and the included command "?ping" does return the expected response "Pong!"
I run a server where we start a video game each month and play through while discussing it along the way, similar to a book club. So I'm trying to create a channel where people can suggest a game using a bot command since I don't trust they could handle following simple rules on their own.
What I'm trying to figure out is how to go about taking a user command of:
?gs "Video Game Title" "Platform"
And having the bot delete the command and repost as:
#user suggested Video Game Title for Platform
While also adding reaction emojis "👍" and "👎" to allow other users to vote.
I'm not asking for anyone to do this for me, but to simply help point me in the right direction of how to code this with Discord in mind using JS (if possible)
Here is my current "bot.js" code:
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `!`
if (message.substring(0, 1) == '?') {
var args = message.substring(1).split(' ');
var cmd = args[0];
args = args.splice(1);
switch(cmd) {
// !ping
case 'ping':
bot.sendMessage({
to: channelID,
message: 'Pong!'
});
break;
// Just add any case commands if you want to..
}
}
});
args = args.splice(1);
This line is incorrect, args (being a string) has no .splice() method. (I often get .slice() and .split() confused, so this happens to me a lot too!)
Instead, use:
args = args.split(" ").slice(1);
The command requires a reason to work, however, it still does not ban even if I mention someone and give a reason. It's like the command isn't recognized!
bot.on('message', async message => {
if (message.content == prefix + "ban") {
if (!message.member.roles.some(r => ["Administrator", "Co-owner"].includes(r.name)))
return message.reply("Sorry, you don't have permissions to use this!");
let member = message.mentions.members.first();
if (!member)
return message.reply("Please mention a valid member of this server");
if (!member.bannable)
return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
var reason = args.slice(1).join(' ');
if (!reason) reason = "No reason provided";
await member.ban(reason);
}
});
Finally got it to work! This was my code at the end:
bot.on('message', message => {
let member = message.mentions.members.first();
if (message.content.startsWith(prefix + "ban")) {
if (!message.member.hasPermission('BAN_MEMBERS'))
return message.reply("Sorry, you don't have permissions to use this!");
if (!member)
return message.reply("Please mention a valid member of this server");
if (!member.bannable)
return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
// V This line has been changed V
var reason = message.content.split(' ').slice(2).join(' ');
if (!reason) return message.reply("Please specify a reason!");
member.ban(reason);
}
});
It was all because of the reason! Thanks to everyone who helped me, this opened doors for a lot more commands for me.
For the kick command, you have to put an argument as a reason. Like this:
var reason = args.slice(1).join(' ');
member.kick(reason);
This is just like the ban command in the second image.
If you need more help or clarification, ask me.
If this doesn't work, make sure your bot has a high enough role in the role hierarchy.
I have a few pictures, all on imgur with direct image link (format: https://i.imgur.com/XXXXXX.jpg), and a Discord bot made with NodeJS.
I send messages like this:
bot.sendMessage({
to: channelID,
message: "My Bot's message"
});
I have tried this:
bot.sendMessage({
to: channelID,
message: "My Bot's message",
file: "https://i.imgur.com/XxxXxXX.jpg"
});
but I only get the text. I have looked it up, and this question was the only one to even come close to saying what I need to do, and it didn't work.
So how am I supposed to do this?
Here is how the bot is created:
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
// My code
}
ClientUser.sendMessage is deprecated, as is the file parameter in its options. You should be using Channel.send(message, options), with files as an array of strings or FileOptions.
bot.on('messageCreate' message => {
message.channel.send("My Bot's message", {files: ["https://i.imgur.com/XxxXxXX.jpg"]});
});
If you want to stick to your deprecated methods, ClientUser.sendFile might be something of interest to you, though I do recommend you move over to the stuff that's more current.
You can send local files in v11.2 like this:
var Discord = require('discord.js');
var bot = new Discord.Client();
bot.on('message', message => {
var prefix = '!'
var msg = message.content;
if (msg === prefix + 'image') {
message.channel.send('Message that goes above image', {
files: [
"./image-to-send.png"
]
});
}
});
bot.login('TOKEN');
Since this is one of the top results on google in 2019, I'm adding the new method of how to upload files with discord.io
First thing that's different is the on() function takes some additional parameters.
Next is that there's a new method called uploadFile that takes an uploadFileOpts object. the file can take a string that is a local path from your bot file to the image.
uploadFileOpts = {
to: string,
file: string|Buffer,
filename?: string,
message?: string
}
So, if you place your image next to your bot script, your code should look like this
bot.on('message', function (user, userID, channelID, message, evt) {
bot.uploadFile({
to: channelID,
file: 'myImage.jpg'
});
}
If you still want to snag that image from the internet, you'll need to convert it into a Buffer object. However, storing the file locally is simpler.
If you're using discord.io instead of Discord.js, refer to this syntax:
https://izy521.gitbooks.io/discord-io/content/Methods/Channels.html
I'm still trying to get it to work.