serverDeafen discordbot | discord/js - discord

ok. I have a discord music bot, and I want to Server Deafen the bot whenever it joins the channel, I already did .setSelfDeaf(true), but couldn't find anything for serverDeafen
here is the code i use as .setSelfDeaf(true) :
await queueConstruct.connection.voice.setSelfDeaf(true);
I also tried this for serverDeafen after that : await queueConstruct.connection.voice.serverDeaf;
but it didn't work!

Are you looking for the .setDeaf() method?
VoiceChannel.join()
.then(connection => {
connection.voice.setDeaf(true)
.catch(err => console.error(err));
})
.catch(err => console.error(err));

Related

I would like my bot to send a dm to the person who and ban

I made my bot etc but I would like to make it so that the bot sends a dm when it ban a person I do not find it has been 3 hours that I search but without success
exports.run = (client, message, args) => {
if (message.member.hasPermission('BAN_MEMBERS')) {
const user = message.mentions.users.first();
let reason = args.slice(1).join(' ');
if (!reason) reason = "No reason provided";
if (user) {
const member = message.guild.member(user);
user.send("You we're banned from **__" + message.guild.name + "__** for **__" + reason + "__**")
.catch(() => message.reply("Unable to send message to user"))
.then(() => member.ban({
reason: "BANNED!"
}))
.catch(err => {
message.reply('I was unable to ban the member');
console.error(err);
});
if (member) {
member
.ban({})
.then(() => {
message.reply(`Successfully banned **__${user.tag}__** for **__${reason}__**`);
})
.catch(err => {
message.reply('I was unable to ban the member');
console.error(err);
});
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to ban!");
}
} else {
message.reply('you don\'t have permission to ban members!')
}
}
There's a few flaws here.
Firstly, message.guild.member() is not a method. You should instead use message.mentions.members.first(); instead of message.mentions.users.first(), then access the User object via GuildMember.user (in your case, user.user, unless you rename the variable) if you need to. You could use guild.members.fetch() but this is much easier.
Secondly, you try to message, if you can't you don't bother banning. Which means if a user blocks your bot, they can prevent getting banned by it. So to fix this, with your current code, just do
user.send("message")
.catch(err => {
message.reply("User could not be messaged");
});
user.ban( {reason: "BANNED!"} )
.catch(err => {
message.reply("User could not be banned");
});
If it continues to error after this, please respond with the error your receive.

How do I get my discord bot to join a vc that a user is in?

I'm trying to get my bot join a vc that a user is in, when saying "+sing". After the audio is finished the bot should disconnect from the vc. This is as far as i've gotten, I don't know what I have done wrong.
client.on("message", msg => {
if (message.content === '+sing') {
const connection = msg.member.voice.channel.join()
channel.join().then(connection => {
console.log("Successfully connected.");
connection.play(ytdl('https://www.youtube.com/watch?v=jRxSRyrTANY', { quality: 'highestaudio' }));
}).catch(e => {
console.error(e);
connection.disconnect();
})
});
Btw I'm using discord.js.
You can get their GuildMember#VoiceState#channel and use that as the channel object to join
client.on("message", msg => {
if (message.content === '+sing') {
// you should add a check to make sure they are in a voice channel
// join their channel
const connection = msg.member.voice.channel.join()
}
})

Discord.js: Creating a channel and doing that a webhook send something when it's created

I'm trying to do a simple discord bot using discord.js that, when a channel is created and has a certain name, a webhook is created in that channel and sends something. I tried with different codes but I have no results (some time ago my code worked, now I don't know why it doesn't work).
Here's the code that I'm using (and worked time ago):
bot.on('channelCreate', channel => {
try {
if (channel.name === 'test') {
var webhook = channel.createWebhook('test webhook')
webhook.send('Hello World')
}} catch (err) {
console.log(err)
}
})
The error that I'm having is this:
TypeError: webhook.send is not a function
Can someone help me with this?
.createWebhook returns a promise, so you have to use .then or use async await, try this:
bot.on('channelCreate', async (channel) => {
try {
if (channel.name === 'test') {
var webhook = await channel.createWebhook('test webhook')
webhook.send('Hello World')
}} catch (err) {
console.log(err)
}
})

My Network Response shows in the console but won't save to my variable

I am using React.js and Axios to call a django api on my local machine. When I make the request and check the network console, the data is there. But when I try to save the response data to a variable and log the results, nothing shows. I have tried using jquery and gotten the same results. It seems I can also only get the response data in the console when I use 'await'.
Portion of my Context-Provider.js code:
async componentDidMount() {
var resp = await axios.get('http://localhost:8000/campaign/')
.then((res) => {
resp = res.data
})
console.log(resp)
this.setState({
campaigns: resp.data,
})
}
Here is my consoles output for the network Response
Thinking about this more, it's probably a bad idea to combine async/await and then. I would suggest you go with one or the other
axios.get('http://localhost:8000/campaign/')
.then((res) => {
this.setState({ campaigns: res.data })
})
OR
const resp = await axios.get('http://localhost:8000/campaign/')
this.setState({ campaigns: resp.data })

Discord.js edit restart message

Is there anyway how i can edit my message after the bot restarted, i want him to send an message, restarting now and after the restart it should edit the message to Done :white_checkmark:
console.log(message.author.tag + ' restarted The bot')
message.reply('You restarted the bot, wait a few seconds') // <-- this message should be edited
bot.channels.get("593824605144088586").send(message.author.tag + ' restarted the bot')
bot.channels.get("593824605144088586").send('---------------------------------------------------')
setTimeout(function () { resetBot() }, 5000);
function resetBot() {
restarted = true;
bot.channels.get("593824605144088586").send('Restarting...')
.then(msg => bot.destroy())
.then(() => bot.login(auth.token));
}
Message.reply() returns a Promise, resolving with another Message. To use the sent message, you have to access the returned value. Mind the scope of restartMsg in the examples below.
console.log(`${message.author.tag} restarted the bot.`);
message.reply('You restarted the bot, please wait.')
.then(restartMsg => {
const logsChannel = bot.channels.get('593824605144088586');
if (!logsChannel) return console.error('Logs channel missing.');
logsChannel.send(`${message.author.tag} restarted the bot.\n---`)
.then(() => {
setTimeout(() => {
logsChannel.send('Restarting...')
.then(() => bot.destroy())
.then(() => bot.login(auth.token))
.then(() => restartMsg.edit('Restart successful.'));
}, 5000);
});
})
.catch(console.error);
Async/await equivalent:
// Asynchronous context (meaning within an async function) needed to use 'await.'
try {
console.log(`${message.author.tag} restarted the bot.`);
const restartMsg = await message.reply('You restarted the bot, please wait.');
const logsChannel = bot.channels.get('593824605144088586');
if (!logsChannel) return console.error('Logs channel missing.');
await logsChannel.send(`${message.author.tag} restarted the bot.\n---`);
setTimeout(async () => {
await logsChannel.send('Restarting...');
await bot.destroy();
await bot.login(auth.token);
await restartMsg.edit('Restart successful.');
}, 5000);
} catch(err) {
console.error(err);
}

Resources