I know that there are a lot of question on this topic, but I browsed all of them but none of the answers seemed to fix my issue. I'm just creating a Discord bot for fun, I have almost never programmed in JavaScript but I wanted to try it.
My Code looks like this:
var url = "https://www.youtube.com/watch?v=HHv-s2OqYpw"
if(!message.member.voice.channel){
message.channel.send("Join a voice channel.");
return
} else {
vc = message.member.voice.channel;
connection = await vc.join();
isValid = ytdl.validateURL(url);
if(!isValid){
message.channel.send("The url you gave doesn't exist");
} else {
const stream = ytdl(url, {filter: "audioonly"});
const dispatcher = connection.playStream(stream)
dispatcher.on("end", function() {
vc.leave()
message.channel.send("Done playing the only music I can play lol")
})
I could do that it could play more songs but I only want it to play this one and in the terminal i get this error:
connection.playStream is not a function
I do have ytdl-core and opusscript installed and they both have the newest version. I tried doing this in lots of different ways, and it really annoys me that I can't figure out what the problem is. Sorry for bad english and thank those who help me. Have a nice day!
https://discord.js.org/#/docs/main/stable/class/VoiceConnection?scrollTo=play
As you can see, there's a VoiceConnection#play() method, and since you're most likely on v12, this is the method you're looking for.
const stream = ytdl(url, options);
const dispatcher = connection.play(stream);
Related
The following code:
const { JsonRpcProvider } = require("#ethersproject/providers")
const { Contract } = require("ethers")
const { Wallet } = require("#ethersproject/wallet");
const abi = require('./abi.json');
const GLOBAL_CONFIG = {
PPV2_ADDRESS: "0x18B2A687610328590Bc8F2e5fEdDe3b582A49cdA",
PRIVATE_KEY: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
BSC_RPC: "https://bsc-mainnet.public.blastapi.io"
};
const signer = new Wallet(GLOBAL_CONFIG.PRIVATE_KEY, new JsonRpcProvider(GLOBAL_CONFIG.BSC_RPC));
const contract = new Contract(GLOBAL_CONFIG.PPV2_ADDRESS, abi, signer)
const predictionContract = contract.connect(
signer
)
predictionContract.on("StartRound", async (epoch) => {
console.log("\nStarted Epoch", epoch.toString())
});
It has been working perfectly for months. However, last night it stopped. No new builds/code changes on my end. I've been trying everything I can think of but nothing. The signer seems to bring back my wallet details ok. I can also see all the functions on the predictionContract but can't get it to return the current Epoch value etc. As you've probably already noticed, I'm not much of a coder, so any help in understanding this would be amazing.
After some more time thinking, I noticed that the contract seemed pretty busy and decided to try a different RPC (I'd already tried 3 before). Seems to be working now, but not exactly scientific. Is there any way to monitor the time requests take and where the lag is? I had a design idea to test a multiple of RPCs during initiation and then use the fastest / most reliable, but no idea where to start with that!
Hello so I am writing a discord bot and all I want is an ordered list of members ordered by their ids. To achieve this I am running following code, however it just console.logs "Couldn't fetch members", with no further errors. :(
Upon further inspection, I discovered that it is a timeout error. Everything else works fine and I think that the const guild isn't the problem, but other than that hope that you people can help, Cheers!
const IDs = new Map();
var repeat = new Boolean(false);
var randomInt = new Number(0);
client.on('ready', () => {
console.log('Ready!');
const guild = client.guilds.cache.get("xxxxxxxxxxxxxxxxxx");
guild.members.fetch().then(members => {
console.log("Found the members");
IDs = members.map(user => user.id);
const OnlineMembers = members.filter(member => member.presence.status == "online");
}).catch(e => console.log("Couldn't fetch members."));
});
P.S. This is running on a Raspberry Pi over Nodemon --inspect, in case that changes anything
I found the solution, sorry if I bothered anybody, there is this really small setting in the Discord Dev Hub. That enables or disables this feature, obviously I was to dumb to turn it on from the start, thank you to anybody who took some time for this, Cheers!
So I'm trying to create a discord bot and one of the commands for the bot needs me to filter roles by the permissions they're allowed (or, at least this would be the easiest way), and I can't seem to figure out how to do that. I've tried way too many things to even count, and at this point, I'm looking for a different way. But if anyone has the answer to this, that would be amazing.
Basically I wanna do this:
let staff = message.channel.guild.roles.cache.filter(s => s has this permission)
(Pseudo-code obviously)
But as I said, I just can't seem to figure it out.
case 'staff':
let staff = chan.guild.roles.cache.filter(m => m.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES));
//console.log(staff)
message.guild.channels.create(Channel, 'text').then (
function() {
let ch = message.guild.channels.cache.find(ch=>ch.name === Channel)
ch.updateOverwrite(message.guild.id, { VIEW_CHANNEL : false });
ch.updateOverwrite(staff, { VIEW_CHANNEL : true })
}
)
type.stop()
break;
Also, 'chan' is a shortened version of 'message.channel'
Is this what you're looking for?
let rolesWithManageMessagesPermission =
message.channel.guild.roles.cache.filter(r => r.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)
No idea why it won't work, the exact code worked on my old bot. Code:
client.on("guildMemberAdd", member => {
const Discord = require("discord.js");
const embed = new Discord.RichEmbed()
.setTitle("**Please be sure to read our rules carefully thanks**")
.setAuthor("Welcome to BACKUP")
.setColor(3447003)
.setDescription("Please enjoy your stay")
.setThumbnail(message.author.avatarURL)
client.channels.get('505107608391254030').send({embed});
})
}
The thing that confuses me most, is that if I replace that code with this code, it works fine.
client.on('guildMemberAdd', member => {
member.guild.channels.get('505107608391254030').send("This works, but embed does not, fix it boi, line 102");
});
(On the code that did not work, I tried: client.channels.get, member.channels.get, member.guild.channels.get, client.guild.channels.get
The problem is when you are finding the channel, client.channels.get isn't a method. doesn't work in this situation for reasons I'm not aware of
You have to use client.guilds.get(GUILD_ID).channels.get(CHANNEL_ID).send({embed});
I want to make a music bot in my command handler, but I ran into some problems.
This is the command handler I use:
delete require.cache[require.resolve(`./commands/${command}.js`)];
let commandFile = require(`./commands/${command}.js`);
commandFile.run(client, message, args);
And in my play.js file I have a queue:
var servers = {};
I don't know how to make it so that I can skip a song (using the skip command - skip.js) in the queue. Code for skipping:
if (server.dispatcher) server.dispatcher.end();
I tried looking at tutorials but they all do it in one file which makes it easier because you can just put the "var servers = {};" on the top and its going to work. I couldn't find any tutorials where they shown how to make it so that you can use a command handler like mine.
Here are all the files:
play.js - https://hastebin.com/dijavugufu.js
skip.js - https://hastebin.com/kupecayotu.js
It would also be nice if someone told me how to modify some other music bot commands to work with a command handler.
Hey man not sure if you're still looking for an answer but I'm also working on a bot with a command handler. The way I got around this was to export the skip function directly from the play file and use that function in the skip file. Here's kinda what I did.
/*In play.js*/
var dispatcher;
async function Play(connection, message){
dispatcher = await connection.playStream("your url and options here");
}
module.exports.Skip = function(){
if(dispatcher) dispatcher.end();
}
/*In skip.js*/
const playModule = require("your_path_to/play.js");
module.exports.run = async (client, message, args) => {
var skip = playModule.Skip();
}
Sorry, I'm still pretty new to Node.js and creating a Discord bot and this may not be the most elegant solution. But the main point is that I got around it by writing the function in play.js and exporting that function to skip.js and calling it there.