Discord x Hastebin command doesn't behave as intended - javascript

So I'm trying to make a bot that posts given data to hastebin.
But it doesn't really behave well. I'm using discord.js and hastebin-gen.
This is the code:
const hastebin = require('hastebin-gen');
exports.run = (client, msg, args) => {
let haste = args.slice(0).join(" ")
let type = args.slice(1).join(" ")
if (!args[0]) { return msg.channel.send("Usage: bin.haste yourmsghere.") }
hastebin(haste, type).then(r => {
msg.channel.send(":white_check_mark: Posted text to Hastebin at this URL: " + r);
}).catch(console.error);
}
When ran, for example bin.haste this code is awesome, it returns this:(https://a.pomf.cat/rkjqog.png) (Note: Can't post images yet, sorry.)
If I click on the link, it successfully uploads the text given to Hastebin: (https://a.pomf.cat/ovcpzb.png)
But there's just that annoying fact that it repeats the given text, as seen at the end of the link, and after the link.

What it's adding is the extension you're passing to hastebin-gen. Don't pass one and it will just respond with the link.
Hastebin-gen only adds the extension to the link at this line:
res("https://hastebin.com/" + body.body.key + ((extension) ? "." + extension : ""));
The extension doesn't matter at all, hastebin.com/beqojuzowe.jhffyjbfft will point to the same as hastebin.com/beqojuzowe.
TL;DR use this:
hastebin(haste).then(r => {

Related

Replace certain character in Discord.JS

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!*');

Message Collector doesn't start when bot DMs user to collect response

So this is moretheless in relation to the previous question, which was answered (the previous one) The bot is no longer spitting out a lot of errors whenever somebody runs the command, and the bot successfully DMs the user the response prompt, but it seems as if the Message Collector isn't starting? The bot DMs the user, nothing spits out in Console, and that's it. You can respond to the bot all day, and it wont collect it and send it to the channel ID. Any pointers?
Here's the code I believe it may revolve around:
collector.on('collect', (message, col) => {
console.log("Collected message: " + message.content);
counter++; ```
And here is all of the code (just in case it actually doesn't revolve around that):
``` if(message.content.toLowerCase() === '&reserveupdate') {
message.author.send('**Thanks for updating us on the reserve. Please enter what you are taking from the reserve below this message:**');
let filter = m => !m.author.bot;
let counter = 0;
let collector = new discord.MessageCollector(message.author, m => m.author.id, filter);
let destination = client.channels.cache.get('my channel id');
collector.on('collect', (message, col) => {
console.log("Collected message: " + message.content);
counter++;
if(counter === 1) {
message.author.send("**Thanks for updating us on the reserve, it is highly appreciated.**");
collector.stop();
}
I think you might be wrong on how you created your message collector.
According to the docs, you should make it this way :
const filter = m => !m.author.bot;
// If you are in an async function :
const channel = await message.author.createDM();
// Paste code here
// Otherwise :
message.author.createDM.then(channel => {
// Paste code here
});
// Code to paste :
const collector = channel.createMessageCollector(filter, { max: 1, time: 60000 });
collector.on('collect', msg => {
// ...
});
Hope this will help you to solve your issue ! :)

How to get a RichEmbed from the channel by field value?

So, I'm creating the bot for my Discord channel. I created a special system based on requests. For example, the user sends a request to be added to the chat he wants. Each request is paired with a unique ID. The request is formed and sent to the service channel where the moderator can see those requests. Then, once the request is solved, moderator types something like .resolveRequest <ID> and this request is copied and posted to 'resolved requests' channel.
There is some code I wrote.
Generating request:
if (command === "join-chat") {
const data = fs.readFileSync('./requestID.txt');
let requestID = parseInt(data, 10);
const emb = new Discord.RichEmbed()
.setTitle('New request')
.setDescription('Request to add to chat')
.addField('Who?', `**User ${msg.author.tag}**`)
.addField('Which chat?', `**Chat: ${args[0]}**`)
.setFooter('Request\'s ID: ' + requestID)
.setColor('#fffb3a');
let chan = client.channels.get('567959560900313108');
chan.send(emb);
requestID++;
fs.writeFileSync('./requestID.txt', requestID.toString(10));
}
Now the .resolveRequest <ID>:
if (command === '.resolveRequest') {
msg.channel.fetchMessages({limit : 100}) //getting last 100 messages
.then((messages) => messages.forEach(element => { //for each message get an embed
element.embeds.forEach(element => {
msg.channel.send(element.fields.find('value', args[0].toString(10))); //send a message containing the ID mentioned in 'args[0]' that was taken form the message
})
}));
}
.join-chat <chat_name> works flawlessly, but .resolveRequest <ID> does't work at all, even no errors.
Any way to fix it?
Using .find('value', 'key') is deprecated, use .find(thing => thing.value == 'key') instead.
Also you should use a DataBase to store things, but your Code actually is not broken, its just that you check for: command === '.resolveRequest', wich means you need to run ..resolveRequest, as in the command variable the prefix gets cut away so change that to: command === 'resolveRequest'

Alexa skill problem with response after response is already returned

I'm working on an Alexa skill that requires a multi-step dialog where the user must answer multiple questions in succession. I'm trying to get started by adding a single slot prompt by checking if the slot is confirmed and returning a response with addElicitSlotDirective if it isn't:
Here is the entire request handler:
const isIntentRequest = require('../utils/isIntentRequest');
const sound = require('../utils/sound');
const loadUser = require('../utils/loadUser');
const loadWordByIndex = require('../utils/loadWordByIndex');
module.exports = {
canHandle(handlerInput) {
return isIntentRequest(handlerInput, 'NewWordPathIntent');
},
async handle(handlerInput) {
const user = await loadUser(handlerInput);
const { wordIndex } = user;
const word = await loadWordByIndex(wordIndex);
const slots = handlerInput.requestEnvelope.request.intent.slots;
if(!hasAnswered('NewWordEnglishAnswer')) {
return handlerInput.responseBuilder
.speak('New Word English Answer word ' + word)
.reprompt('New Word English Answer word ' + word + ' reprompt')
.addElicitSlotDirective('NewWordEnglishAnswer')
.getResponse();
}
return handlerInput.responseBuilder
.speak('You answered the new word english question')
.getResponse();
function hasAnswered(slotName) {
const slot = slots[slotName];
if(!slot) throw new Error(`Invalid answer slot: "${slotName}"`);
return slot.confirmationStatus === 'CONFIRMED';
}
},
};
This seems to mostly be working. When this intent is handled, Alexa responds with: New Word English Answer word evidence which is correct, but then she immediately follows it with There was a problem with the requested skill's response and kills the session.
Why is this happening? If the response is being output, how is there a problem with the response?
Use console.log to write things to the log and configure your skill to use Amazon's CloudWatch Logs to see the log. Even without you writing statements to console.log yourself, there will be info about the "problem with requested skill's response".
Even without that, you can often get the information about the problem if you use the Alexa Developer Console Test tab to test your skill. It may show you something in the JSON Input or JSON Output that will give you a clue.

How to properly tag a user using message.channel.send in discord.js

How to properly tag a user using message.channel.send in discord.js?
Here is my code:
function replaceAll(str, find, replacer) {
return str.replace(new RegExp(find, 'g'), replacer);
}
Bot.on('message', (message) => {
var mcontent = message.content;
var mauth = message.author;
var mtag = mauth.tag;
if (mcontent.includes("#p")) {
var newmsg = replaceAll(mcontent, "#p", "#" + mtag);
message.delete();
message.channel.send(newmsg);
});
And, it prints this: (By the way, I am hieyou1#6009) [with the message.delete(); disabled]
No console logs are present when I execute.
Mentions from the bot is a little bit different. You need to use <#userid>.
But Discord.JS has a cleaner way to mention a user, instead of using message.author.tag, just use message.author. That will tag the user that sent the message.
message.channel.send(`Hey ${message.author} how's it going?`);
Or with the old way to concatenate the string:
message.channel.send("Hey " + message.author + " how's it going?");

Categories

Resources