Allowing links only in a specific channel - discord

I was wondering if any of you guys is able to help me out here, i'd like to make a channel to be links-only, meaning if you try to type or send a message there it will get deleted by the bot saying something like "ERROR! this channel is for links only" just like when you do a filter for links to be deleted. Thank you to whoever could provide any sort of help and examples.

in your message event you can check if the message was sent in the only-links channel if so check message.content against a RegExp() to determine whether it should be allowed or not.
if (message.channel === message.guild.channels.find(channel => channel.name === 'links-only')) {
const linkRegex = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)/g)
if (!linkRegex.test(message.content)) {
message.delete()
message.reply('this is a link-only channel').then(msg => msg.delete(5000))
}
}

Related

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

How to not send bots message edit discord.js

I have a message edit log but I want to stop sending the log if a mobs message was updated, I tried a few codes like
if(bot.oldMessage.content.edit()){
return;
}
It showed and error
cannot read property 'edit' of undefined
I then removed edit then content was undefined. The code for the message update is below.
The Code
module.exports = async (bot, oldMessage, newMessage) => {
let channels = JSON.parse(
fs.readFileSync('././database/messageChannel.json', 'utf8')
);
let channelId = channels[oldMessage.guild.id].channel;
let msgChannel = bot.channels.cache.get(channelId);
if (!msgChannel) {
return console.log(`No message channel found with ID ${channelId}`);
}
if (oldMessage.content === newMessage.content){
return;
}
let mEmbed = new MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL({dynamic: true}))
.setColor(cyan)
.setDescription(`**Message Editied in <#${oldMessage.channel.id}>**`)
.addField(`Before`, `${oldMessage.content}`)
.addField(`After`, `${newMessage.content}`)
.setFooter(`UserID: ${oldMessage.author.id}`)
.setTimestamp()
msgChannel.send(mEmbed)
}
How would I stop it from sending the embed if a bots message was updated.
Making a really simple check will resolve this issue. In Discord.js there is a user field that tells you if the user is a bot or not.
In fact, it is really recommended you add this in the "onMessage" part of your code as it stops other bots from using your bot, this is to make sure things are safe and no loopbacks/feedbacks happen, either way, you don't want a malicious bot taking advantage of your bot, which can get your bot in trouble too.
Here is what you want to do;
if (message.author.bot) return;
What this code specifically does is check if the message's author is a bot, if it returns true, it will break the code from running, if it returns a false, the code continues running.
You can do the same if you want to listen to bots ONLY by simply adding a exclamation mark before the message.author.bot like this;
if (!message.author.bot) return;
It is also possible to see what other kinds of information something holds, you can print anything to your console. For example, if you want to view what a message object contains, you can print it into your console with;
console.log(message) // This will show everything within that object.
console.log(message.author) // This will show everything within the author object (like ID's, name, discriminators, avatars, etc.)
Go ahead and explore what you can do!
Happy developing! ^ -^
That is really easy to do. All you need to do is check if the author of the message ist a bot and then return if true. You do that like this
if (oldMessage.author.bot) return;

How to detect when a user joins a voice channel?

I am currently trying to make a bot for my server and one of the things I want to do is have it detect whenever a user joins any voice channel and simply make it send a message. I can't personally figure it out or find any answers on the internet as most of the time people are detecting it based on a command while I want it to be passive. I know that voiceStateUpdate has been changed and some things are different from how I've seen others use it in the past.
If anyone has any solutions please let me know, thanks.
client.on('voiceStateUpdate', (oldState, newState) => {
if (newState.channelID === null) console.log('user left channel', oldState.channelID);
else if (oldState.channelID === null) console.log('user joined channel', newState.channelID);
else console.log('user moved channels', oldState.channelID, newState.channelID);
});
In discord.js v.12 the listener you need to use is indeed voiceStateUpdate. It has the parameters oldState and newState. With those you can detect a number of things including the member object.
Using that you might use something like this to detect if a user or bot is connecting or disconnecting a voice channel.
client.on('voiceStateUpdate', (oldState, newState) => {
// check for bot
if (oldState.member.user.bot) return;
// the rest of your code
})

how do i store a user in a variable in discord.js

so, I am running a boy scout server and for it, I am making a bot. we have been struggling to get everyone's real name for better organization. the bot will ask for their name and they will type !name Travis_c. then I want it to store their discord username in a variable and send me a message saying trullycool => Travis_C. with that information, it would be super simple to change their nickname on the server. I am running into issues with getting the user who sent the previous message, into a variable.
my code is
`
case 'name':
name = args[1];
const channel = message.guild.channels.cache.find(channel => channel.name === "names");
user = message.author()
if(!channel) return;
message.channel.send(`${user} => ${name}`)
`
I am having issues on user = message.author()
I know if I want to reply to something I do message.reply but that wouldn't work in my situation. thanks in advance!
message.author is not a function so there is no need for () at the end of it.

Discord.js moving person to random VoiceChannel

I'm making a discord bot that will has a feature to move user's to a random voice channel if they want. I searched the internet 3 hours straight and checked the whole documentation. But still can't find anything.
note : I know this bot idea looks useless. But that's what I need.
code :
let voiceChannels = message.guild.filter(g => **idk how to check if it's vc** );
that's what I just found in 3 hours.
You'd need to access the guild's channels and then choose a random channel of type voice.
So in your case, it'd be:
let voiceChannel = message.guild.channels.cache.filter((channel) => channel.type === "voice").random();
if (!message.member.voice.channel) return message.reply("User is not in a voice channel");
await message.member.voice.setChannel(voiceChannel);

Resources