Music Bot DiscordJS v13 - discord.js

My problem is that if the user is not in the same channel as the bot, the user will not be able to disconnect it. Also, if the user is in the same channel, it can disconnect it.
const voiceChannel = message.member.voice.channel;
const { channel } = message.member.voice || message.member.voice.channel;
const guildQueue = client.queue.get(message.guild.id);
// Check if the bot is in a channel.
if (!guildQueue || !message.guild.me.voice.channel) {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`I'm not on a voice channel right now.`)
return message.channel.send({ embeds: [embed] });
}
// Check if the user is in the same channel. If not then it will not be able to disconnect the bot.
if (!message.guild.me.voice.channel == guildQueue){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`You're not on the call.`)
return message.channel.send({ embeds: [embed] });
}
// Disconnects the bot from the call.
if (message.guild.me.voice.channel = guildQueue){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`I'm no longer connected.`)
guildQueue.connection.destroy();
client.queue.delete(message.guild.id);
return message.channel.send({ embeds: [embed] });
}
}

When you use = it attribiute things to variables
in if/else statments we use === to check if something is true or false
Soo from that you need to change if statment from
if (message.guild.me.voice.channel = guildQueue)
to:
if (message.guild.me.voice.channel === guildQueue)
If something is wrong (grammatically) please edit this answer!

There's 2 issues in this code.
Firstly, you're attempting to set the value of message.guild.me.voice.channel to guildQueue by the use of 1 equal sign.
Secondly, you're trying to compare the voice channel to your queue, which is most likely a queue of all the songs picked by the guild, which means it wont properly compare to your voice channel. There's 2 cases of this issue, once in the !message.guild.me.voice.channel == guildQueue if statement and second in the message.guild.me.voice.channel = guildQueue if statement. Neither of these will ever return true since guildQueue will never equal a channel. A fix for this would be to check if the user is in the same channel as the bot and the second if statement is pretty much irrelevant since you've already checked that the user is in the same channel with the bot and the bot is in the channel that the music is being played in.

Related

Is there an easy way to make my bot mention the person its moving?

so basically I wanted to make my bot move people to afk as soon as they deafen. and I have a command to make it generate messages in chat, but the question is, can I make it # them as well? and if so how?
code:
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_VOICE_STATES", "GUILD_MEMBERS", "GUILD_PRESENCES"] });
client.on("voiceStateUpdate", (oldState, newState) =>
{
if (newState.selfDeaf)
{
console.log('User has deafened');
newState.member.voice.setChannel("695840093167943751");
client.channels.cache.get("664487959940169738").send('Undeafen Bitch');
}
To tag a user in discord, the format is <#USERID> so if a user's id is 1, you'd have to include <#1> in what you are sending.
So onto your code, you'd have to change the last line to something like the following:
client.channels.cache.get("664487959940169738").send('<#123> Undeafen Bitch');
But we can't hardcode the id since it'll be unique for each user moved. This should automatically tag the user who got moved:
client.channels.cache.get("664487959940169738").send(`<#${newState.member.id}> Undeafen Bitch`);

(Discord selfbot) Is there anyway to response if 'hi' in someone message

Is there anyway to make the selfbot response 'hello' if 'hi' is in someone message?
Like
A: hi? Anyone here?
The bot: hello
The bot response because 'hi' is in A's message.
I think it's not possible because i tried many Discord selfbot library and none of library work (Python)
Which library are you using?
I made a discord bot with discord.js (Javascript), and with that you can receive an event every time someone talks in your discord server and then respond depending on the contents of the message.
First you start your discord client (the Intents may vary depending on what you want to do):
const discordClient = new Client({ intents: [Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MESSAGES] })
Then the idea is to get the content of every new message and answer properly, once you have created your discord client, you will need to set up an event listener like this one:
discordClient.on('messageCreate', message => {
const content = messageReceived.content.toLocaleLowerCase()
if (content === 'hi') {
messageReceived.channel.send("hello, I'm a bot!")
}
})
And don't forget to login with your discord bot key
const discordKey = "YOUR DISCORD BOT KEY"
discordClient.login(discordKey)
You can also see the repository for my bot here.
And for your use case you would want to mainly focus on some parts insid discord.ts.
Hopefully this helps.
Here are some basic examples using discord.js within node.js.
Simple Response
To send a response, simply check if the user's message contains the desired input you want your bot to respond to.
if(message.content.toLowerCase().includes("ping") {
message.reply("Pong!")
}
Multiple Responses
This is a much more efficient method of responding to multiple inputs. This includes using an array.
const array = [
"hello",
"hola",
"bonjour"
]
if(array.some(word => message.content.toLowerCase().includes(`${word}`)) {
message.reply("Hello!")
}
Here's the code
---Starts From Here:---
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// Only respond to messages containing "hi", "hey", "hello", or "sup"
if (context.params.event.content.match(/hi|hey|hello|sup/i)) {
let messageContent = context.params.event.content.match(/hi|hey|hello|sup/i);
await lib.discord.channels['#0.3.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `${messageContent[0]} to you too 👋`,
message_reference: {
message_id: context.params.event.id
}
});
}
--Ends--
Image of code
I suggest using autocode.com it will really help you in coding life. I use it myself.
Here's a small guide https://autocode.com/guides/how-to-build-a-discord-bot/
If your banned using discord selfbot, that's not my fault
from discord.ext import commands
client = commands.Bot(
command_prefix='-',
self_bot=True,
help_command=None
)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'Hello!':
await message.channel.send("Hi how your day!")
client.run('user token', bot=False)

Discord: Reading messages from one channel in a (non-admin perm) server, and posting it to another (with admin perms)

I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel

Give Role when a member joins the voice channel. discord.js

Im trying to make discord.js code. When a member joins the voice channnel I want the bot to give them a role, when the member leaves, i want the role to be removed. thanks for your help.
const newChannelID = newState.channelID;
const oldChannelID = oldState.channelID;
if (oldChannelID === "835280102764838942"){
member.send('role given');
let role = message.guild.roles.cache.find(r => r.id === "835279162293747774");
member.roles.add(835279162293747774);
} else if(newChannelID === "835280102764838942"){
member.send('role removed');
member.roles.remove(835279162293747774);
}
})
There's a couple of things you need to clean up in your code. I'm going to assume you're using the voiceStateUpdate event to trigger your command.
Most importantly, (logic-wise) you actually need to flip-flop the way you add and remove roles. Currently, the general code states that if the user left the voice channel (thus oldChannelID = actual vc ID), then it would actually give the user a role. This seems to be the opposite of your intention, thus you'd simply swap out the code in each if/else if statement.
Second, the way you're assigning roles and adding them is incorrect. I'd recommend this resource for more info.
Third, you cannot access message if you were indeed using voiceStateUpdate as your event, since it only emits an oldState and a newState.
Lastly, you need to designate a text channel to send the message to. In my code, I manually grabbed the ID of the channel I wanted and plugged it into the code. You'll have to do the same by replacing the number strings I have with your own specific ones.
With that being said, here's the correct modified code:
client.on('voiceStateUpdate', (oldState, newState) => {
const txtChannel = client.channels.cache.get('803359668054786118'); //manually input your own channel
const newChannelID = newState.channelID;
const oldChannelID = oldState.channelID;
if (oldChannelID === "800743802074824747") { //manually put the voice channel ID
txtChannel.send('role removed');
let role = newState.guild.roles.cache.get("827306356842954762"); //added this
newState.member.roles.remove(role).catch(console.error);
} else if (newChannelID === "800743802074824747") {
txtChannel.send('role given');
let role = oldState.guild.roles.cache.get("827306356842954762"); //change this somewhat
oldState.member.roles.add(role).catch(console.error); //adding a catch method is always good practice
}
})

discordjs problems logging whenever a dm is sent

I have a whole logging system that logs whenever various things are done in the server or with the bot, one being a log of whenever a dm is sent to the bot. the log would contain the author ID and the message content. the problem is with logging the message content. the bot crashes whenever a second DM is sent but works fine if its a first DM. the bot crash message is the following: https://gyazo.com/5ad0b41648f83a855ac8c49fb220a612
but the strange thing is, my fields aren't empty:
bot.on('message', msg=>{
const LogChannel = bot.channels.cache.get('712811824826941460');
const LogEmbed = new Discord.MessageEmbed()
.setColor('#606060')
.setAuthor(msg.author.tag)
.setDescription('DM Sent')
.addField('Message', msg.content)
.setTimestamp()
if(msg.channel.type === 'dm')
LogChannel.send(LogEmbed)
});
the console thinks that the .addField('Message', msg.content) is empty but as you can see, it's not. keep in mind it only gives me the error message after a second DM is sent following a first one.
any ideas?
There is no Message.content properties. I guess you are looking for Message.cleanContent
bot.on('message', msg=>{
const LogChannel = bot.channels.cache.get('712811824826941460');
const LogEmbed = new Discord.MessageEmbed()
.setColor('#606060')
.setAuthor(msg.author.tag)
.setDescription('DM Sent')
.addField('Message', msg.cleanContent)
.setTimestamp()
if(msg.channel.type === 'dm')
LogChannel.send(LogEmbed)
});

Resources