How to listen for a mention of CurrentUser as a Discord bot - discord

I want my bot to catch when other users mention my bot in chat. I've tried
message.MentionedUsers.contains(client.CurrentUser)
But this returns false, I believe because MentionedUsers is a collection of SocketGuildUser, while client.CurrentUser is a SocketUser
Do I need to loop through MentionedUsers to look for the user.id? Or is there a more direct way?
Using Discord.Net 3.8.1

I cant explain why your solution is not working but I have a other way for you to check if your bot is in the mentioned users using linq:
//you have to add this using if you havent yet
using System.Linq;
if(message.MentionedUsers.Any(m => m.Id == client.CurrentUser.Id))
{
//this code will be fired if your bot was mentioned in the message
}

Related

How to (and is it possible to) make link markup work in a discord bot message?

What I'm trying to achieve is simple:
send a message like links: [link1](https://example.com/1), [link1](https://example.com/2) via a bot
get it displayed as "links: link1, link1"
In a ephemeral follow up, it works as expected:
const content = "links: [link1](https://example.com/1), [link1](https://example.com/2)"
await interaction.followUp({
ephemeral: true,
content,
})
But when I send this to a public channel like this:
await channel.send(content)
I'm getting it as plain text (links: [link1](https://example.com/1), [link1](https://example.com/2)) except the links are clickable.
Is it possible to get the same result as in an ephemeral message?
I've checked the permissions, there's only "embed" links (which sounds like "allow links in embeds", and not "allow links in messages) and it is enabled anyway on server for everyone, for the bot and in the channel. So what am I missing here?
PS Here they say that "it's only possible if the message was sent with webhook though", but I'm not quite sure what does this mean (can this be different for a public and an ephemeral message?)
You cannot use hyper links in normal messages sent by a bot. You need to use a Webhook. Considering you're using discord.js, see this guide on their documentation. When using something like that it will work as expected
const { WebhookClient } = require('discord.js');
const webhookClient = new WebhookClient({ url: "WEBHOOK_URL" });
webhookClient.send({
content: "[Hello world](https://github.com)",
username: "Webhook Username",
});
Otherwise you may use embeds and send one of these with your bot and the content you wish to have.
Right, so I've found a solution based on suggestion by Krypton. As an embed doesn't look nice and manual creation of a webhook is not an option for me, I've found that I can create a webhook on the go and use it.
After some testing, I've figured that such webhooks are also permanent (although they can be found in another part of settings than the manually created ones); they should be deleted as there's a limit of 15 webhooks – an attempt to create more fails (with DiscordAPIError[30007]: Maximum number of webhooks reached (15)).
Instead of doing
await channel.send(content)
I've put
// a guard, TypeScript requires us to be sure
if(channel instanceof TextChannel) {
const webhook = await channel.createWebhook({
// a message from a webhook has a different sender, here we set its name,
// for instance the same as the name of our bot (likewise, we can set an avatar)
name: "MyBot",
})
await webhook.send(content)
await webhook.delete()
}
This seems to be a minimal code change to get the links working.

discord.js make a bot delete a given number of messages

I've started creating a discord bot and I've wanted it to start moderating my server. my first command to try was 'mass delete' or 'purge' which deletes the number of messages a user gives.
But I'm pretty new to java and discord.js so I have no idea how to start.
// code here to check if an mod or above
// code to delete a number of message a user gave
}```
For permissions checking, you could simply check for the message author's roles using:
if (!message.member.roles.cache.has(moderator role object)) return;
Overall, you should be looking at the bulkDelete() function that can be found here.
Although, since you don't really know how to really develop bots yet from what you're saying, I won't be telling you the answer to your problem straight ahead. I'd highly recommend learning some JavaScript basics (Also, keep in mind Java != JavaScript) as well as Discord.js basics before you jump head straight into what you want to create.
I'd highly recommend WornOffKeys on YouTube, they're fantastic.
Check user permission first using
if (!message.member.hasPermission("MANAGE_MESSAGES")) return message.channel.send("You have no permission to access this command")
check bot permission using
if (!message.guild.me.permissions.has("MANAGE_MESSAGES")) return message.channel.send("This bot have no permission to delete msg!")
return a message when user havnt mention a select message
if (!args[0]) return message.channel.send("no messages have been selected")
then as Bqre said using bulkdelete()
try {
await message.channel.bulkDelete(args[0])
} catch (e) {
console.log(e); //err
}
hope you find it useful!

Java Discord Bot: dont execute code in onGuildVoiceMoveEvent-Listener when Member gets moved by the bot itself

#Override
public void onGuildVoiceMove(#NotNull GuildVoiceMoveEvent event) {
}
I want that the code inside this Eventlistener doesnt get executed
when the bot itself moves the member(not gets moved). I tought of
using something like this:
if(!event.getMover().getUser().isBot()){ //getMover doesn't exist just
//.getMember and .getEntity
//Code
}
But i can't find a way to get the Member/User that moves the Member.
I'd be very thankful if anyone has any ideas.
This is not Supported by the Discord JDA / Discord API. You can open a Suggestion Issue here or a Suggestion for the official Discord API here

Discord,js bot making a channel

I am trying to make my discord bot create a channel! I have tried many way's but none of them work! Note: this is discord.js! Here is the code I have come up with! IT DOES NOT WORK!
guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
'guild' must be defined in some way. You can get it from a message for example by using message.guild or even by its name using client.guilds.find(guild => guild.name === "Guild Name");

Is there a way to check if discord bot joins a server?

I'm making a discord bot and I want to check if it joins a server so I can set prefixes, send messages etc.
I tried searching for this but I didn't find anything that could help.
I thought it could be an event so tried something like:
client.on('join_guild', (guild) => {
prefix = "!"
});
But, of course, it didn't work.
Is there something like the code shown above?
I think you're looking for the guildCreate event. It gets triggered once your bot joins a new guild. Example code:
client.on("guildCreate", (guild) => {
// This event triggers when the bot joins a guild.
console.log(`Joined new guild: ${guild.name}`);
});

Resources