How to send DM messages after triggering comand Dsharp+ - dsharp+

I'm making a Discord bot and i'm in trouble. Idk how to send direct message after triggering command by user, i tried to use RequireDirectMessage, but after this command just became inactive, i couldn't trigger it neither in discord Guild nor in direct message.

You need to create a DM channel and then send a message to that channel like this
[Command("senddm"]
public async Task SendDMExample(CommandContext ctx)
{
var DMChannel = await ctx.Member.CreateDmChannelAsync();
await DMChannel.SendMessageAsync("WhateverYouAreSending");
}

Related

BotFramework-DirectLine JS - Initial Message Missing

I have a Bot I have built with MS BotFramework, hosted on Azure. The Bot is built to start the convo with a Welcome Message. When I test the bot through emulator, or on Azure test webchat, the bot will initiate the conversation as expected with the welcome message.
However, in my chat client using BotFramework-DirectLineJS, it isn't until I send a message that the bot will respond with the Welcome message (along with a response to the message the user just sent).
My expectation is that when I create a new instance of DirectLine and subscribe to its activities, this Welcome message would come through. However, that doesn't seem to be happening.
Am I missing something to get this functionality working?
Given this is working for you on "Test in Webchat", I'm assuming your if condition isn't the issue, but check if it is if (member.id === context.activity.recipient.id) { (instead of !==). The default on the template is !== but that doesn't work for me outside of emulator. With === it works both in emulator and other deployed channels.
However, depending on your use cases you may want to have a completely different welcome message for Directline sessions. This is what I do. In my onMembersAdded handler I actually get channelId from the activity via const { channelId, membersAdded } = context.activity;. Then I check that channelId != 'directline' before proceeding.
Instead, I use the onEvent handler to look for and respond to the 'webchat/join' event from Directline. That leaves for no ambiguity in the welcome response. For a very simple example, it would look something like this:
this.onEvent(async (context, next) => {
if (context.activity.name && context.activity.name === 'webchat/join') {
await context.sendActivity('Welcome to the Directline channel bot!');
}
await this.userState.saveChanges(context);
await this.conversationState.saveChanges(context);
})
You'll still want to have something in your onMembersAdded for non-directline channel welcome messages if you use this approach.

How can you detect if a user replied to a message of a bot in discord.js?

I have searched the discord.js documentation but I can't find a way to make the bot detect if their own messages were replied by a user in a discord server. Is there a way to do this?
Thank you so much,
Paul10
All messages have a message_reference property. This contains the guild, channel and message id. This property is null if it isn’t a reply. I assume you already know the channel.
//async function
//message is an instance of Message
let { channel, message_reference: reply } = message
const repliedTo = await channel.messages.fetch(reply.message_id);
//repliedTo is now the replied message with author, content, etc

How To Send A Discord.JS Startup Message To All Servers

I wanted to inquire on something I’ve been trying to do recently. I have a Discord.JS bot, and it’s pretty public (130+ servers), and I want it to send a message to any channel in every single server it’s in, when the bot starts up, the purpose is to create a “New Update” notification in one of the channels.
Please do not plan on doing this, as this will get you rate limited really quickly.
This is kind of possible. There is a problem, there is no default guild channel, so you cannot get their "main" channel just by their guild object. If you would want to do that you, would need them to select their channel and store that ID somewhere.
This is a simple solution, where you loop through every guild of your Discord client, and send some channel a message. You don't know what channel with this solution specifically, but you will send some channel a message if it is not a voice channel.
const Discord = require("discord.js");
require("dotenv").config();
// Creating our Client
const client = new Discord.Client();
// Event listener: when the bot is ready
client.on("ready", () => {
// Looping through every guild the bot is in
client.guilds.cache.forEach((guild) => {
// Getting one of their channels
let channel = guild.channels.cache.array()[2];
// Sending the channel a message
channel.send("Hey");
});
});
client.login(process.env.DISCORD_BOT_TOKEN);

Telegram: Cannot send message/photo into a channel with Telegraf (NodeJs)

I would send a message on telegram channel with telegraf. I'v einvited the bot and put him admin.
I've tested with this code:
bot.on('text', (ctx) => {
// Explicit usage
ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`)
// Using context shortcut
// ctx.reply(`Hello ${ctx.state.role}`)
})
bot.launch();
But it replies only if i wrote on private.
So why it doesn't work on a channel?
Than how can i send a message in that channel without a command? (For example with and interval?
I i try this one:
bot.use((ctx) => {
console.log(ctx.message)
})
when i use the bot on private chat (with him) it returns all the message data. On the channel i receive undefined
In your case CTX have current chat info, if you want to send message to channel provide correct id as documented for Telegraf sendMessage:
telegram.sendMessage(process.env.TELEGRAM_CHANNEL, ctx.message.text);
I am using a bot for a public channel, so in my case it's:
TELEGRAM_CHANNEL=#MY_PUBLIC_CHANNEL_NAME
The channel name is available in channel info settings t.me/MY_PUBLIC_CHANNEL_NAME

Telegram bot won't send message to a user other than who triggered the bot. [sendMessage not working]

const chat_id = req.body.message.chat.id
const dharmaraj = 2xx4xxx5x //My Telegram UID
So I have these 2 const declared at top and chat_id is ID of group/user who used my bot command. If I use the bot from my personal chat, it does reply. But when someone else use the bot in private or in group, the bot won't alert me about it in Telegram.
Here is the code:
console.log(`Post command used by ${user_id}`)
res.status(200).send({
method: 'sendMessage',
dharmaraj,
text: `Message posted successfully!!`,
parse_mode: 'Markdown'
})
The console.log() statement does work.
But I don't receive the message from the bot.
I have started the bot and have a long chat history as well. So that shouldn't be an issue.
I read this but I don't want to use any other package.
EDIT:
(1) If I replace it with the chat_id itself (ID where the bot was triggered) it will send message in that chat. Problem is to get some text in my personal chat so I know the bot has been used.
(2) I tried embedding the snippet in a try-catch but no error there.
(3) Tried replacing UID with the #username but no luck.

Resources