Discord.Net - Add reaction to previous message from command - c#

From a command, I want to be able to grab the previous message from the chat and add reactions to that message. I am using this line of code to get the previous message but it returns a IEnumerable<IMessage>.
var messages = await Context.Channel
.GetMessagesAsync(Context.Message, Direction.Before, 1)
.FlattenAsync();
IMessages do not have an add reaction function. I tried going through multiple ways to get the socket user message from the last message but can not find any way to get a socket user message from an IMessage.

Simply cast to an IUserMessage.
If cache isn't enabled you won't be able to get a socket message, you'd get a rest message instead. That said, a socket message isn't required, so the interface should suffice.

Related

Deleting Messages a bot has sent Discord.net

I am attempting to create a bot that has the ability to delete only the bots messages after a command has been done.
I am aware of how i know that the command has taken place however I am having issues with only deleting the messages that the bot has sent.
await Message.ModifyAsync(msg => msg.Content = "test [edited]");
However this only occurs to the last message that has been sent (This can be fixed relatively easily and I know how to do this) and importantly would otherwise occur to all the messages in the chat! What i want to do is make it so that I only delete messages that were sent by the bot in the first place.
Thanks
Get and store the message that the bot sent as a variable, and delete it later.
var botMsg = await ReplyAsync("A message!");
await botMsg.DeleteAsync();
If you need to delete multiple messages, create a list and store each message to the list. Then use await Context.Channel.DeleteMessagesAsync(list) at the end of the command.
EDIT
There are overload methods for passing both list of message ID and the message itself.
Example of passing a list of message ID:
List<ulong> msgToDel = new List<ulong>();
msgToDel.Add((await ReplyAsync("test1")).Id); //Send a msg, then add the msg ID to the list.
msgToDel.Add((await ReplyAsync("test2")).Id);
//Blah...
await Context.Channel.DeleteMessagesAsync(msgToDel);
You can check the documentation for the DeleteMessagesAsync() here.

Handling poison messages in MSMQ

Current Setup includes a windows service which picks up a message from the local queue and extracts the information and puts in to my SQL database.According to my design
Service picks up the message from the queue.(I am using Peek() here).
Sends it to the database.
If for some reason i get an exception while saving it to the database the message is back into the queue,which to me is reliable.
I am logging the errors so that a user can know what's the issue and fix it.
Exception example:If the DBconnection is lost during saving process of the messages to the database then the messages are not lost as they are in the queue.I don't comit untill i get an acknowledgement from the DB that the message is inserted .So a user can see the logs and make sure that the DBconnection exists and every thing would be normal and we dont lose any messages in the queue.
But looking into another scenario:The messages I would be getting in the queue are from a 3rd party according a standard schema.The schema would remain same and there is no change in that.But i have seen some where i get some format exceptions and since its not committed the message is back to the queue.At this point this message would be a bottle neck for me as the same messages is picked up again and tries to process the message.Every time the service would pick up the same message and gets the same exception.So this loops infinitely unless that message is removed or put that message last in the queue.
Looking at removing the message:As of now if i go based on the format exception...then i might be wrong since i might encounter some other exceptions in the future .
Is there a way i can put this messages back to the queue last in the list instead beginning of the queue.
Need some advice on how to proceed further.
Note:Queue is Transactional .
As far as I'm aware, MSMQ doesn't automatically dump messages to fail queues. Either way you handle it, it's only a few lines of code (Bill, Michael, and I recommend a fail queue). As far as a fail queue goes, you could simple create one named .\private$\queuename_fail.
Surviving poison messages in MSMQ is a a decent article over this exact topic, which has an example app and source code at the end.
private readonly MessageQueue _failQueue;
private readonly MessageQueue _messageQueue;
/* Other code here (cursor, peek action, run method, initialization etc) */
private void dumpToFailQueue(Message message)
{
var oldId = message.Id;
_failQueue.Send(message, MessageQueueTransactionType.Single);
// Remove the poisoned message
_messageQueue.ReceiveById(oldId);
}
private void moveToEnd(Message message)
{
var oldId = message.Id;
_messageQueue.Send(message, MessageQueueTransactionType.Single);
// Remove the poisoned message
_messageQueue.ReceiveById(oldId);
}

Cant consume message from Topic in activemq

I am new to activemq. T want to ask a question about the topics of Activemq. I succeed to get a message from a queue. Also I can send message to topic/Queue, but I can't get a message from Topic.
I have tried using Java Code. The result is the same.
The following is my core code:
connection.ClientId = clientId;
connection.Start();
using (ISession session = connection.CreateSession())
{
ITopic topic = new Apache.NMS.Commands.Topic(topicName);
IDestination destination = SessionUtil.GetDestination(session, topicName,
DestinationType.Topic);
using (IMessageConsumer consumer = **session.CreateDurableConsumer**(topic, "news", null, false))
{
**consumer.Listener += new MessageListener(consumer_Listener);**
//**IMessage iMsg = consumer.Receive();**
// if (iMsg != null)//{
// ITextMessage msg = (ITextMessage)iMsg;
// return msg.Text;
// }
//else
//return iMsg;
}
}
I also using: IMessage iMsg = consumer.Receive();
IMsg always null(topicname has messages. How can I consume topic's message?
The Messages would need to have been sent after the Topic consumer was created. A Topic is fire and forget, if there are no consumers then the message is discarded. Any consumer that comes online will only receive message sent after that time unless it is either a Durable Topic consumer or a Queue consumer.
In the case of a durable consumer you must have created an instance of it so there is a subscription record before those message were sent to the Topic. So I would guess your problem is that you didn't subscribe this consumer before and so the Broker was not storing any Messages for it.
I was so stupid about the phrase "using".Beacause I use "using" open connection and session. when the code block was excuted, the connnection/session is disappear. Now I dont use "using" block to cerate connection. just like normal code. It works. also I build "Global.asax" file. The program can listener Topic once started up. At the same time, I write a function to colse the connection.I tested. Once a message was sent to the topic, the Onessage() function would be exectued.
just resolve my problem.maybe you would have better answer.Thanks Tim.

ActiveMQ 5.7.0 Selector not working in C#

I have a very simple ActiveMQ message consumer that's created in C# as follows:
using(IMessageConsumer consumer = session.CreateConsumer(destination,"NMSCorrelationID='<value of correlation id>'")){
/* This Receive(..) operation does not retrieve the message with the correlation id which I confirmed to be available on the queue. */
IMessage message = consumer.Receive(new TimeSpan(1000));
}
However, I can get the message if I don't use the selector while creating the consumer. The destination is a queue on the ActiveMQ broker. I've tried using CorrelationID and JMSCorrelationID as the selectors, but none on them worked. The ActiveMQ broker was installed with out-of-the-box settings. Is there any special setting that I need to use for the selectors to work?
You definitely want to set the selector with JMSCorrelationID. Using NMSCorrelationID, or just CorrelationID will cause it to ignore all of the messages. I tested the following out with both topics and queues, and everything worked correctly. I did test on ActiveMQ 5.8.0, but I'm pretty sure this will work just fine on 5.7.0.
IMessageConsumer subscriber = session.CreateConsumer(
"queue://TestCorrelation",
"JMSCorrelationID = 'FOO'",
false);
The broker will not enqueue a message to a consumer from the same connection as the producer if that consumer has set the third parameter (noLocal) to true. You will need to have two separate connections to get the correlation ID selector to work. One to send the message, a consumer on another connection to receive the message. If you set noLocal to false, then a consumer on the same connection as the producer will receive the message.
You can also try using some wildcards in the selector if you want to test.
"JMSCorrelationID LIKE '%FOO%'"
Be aware that the selector is case sensitive. Your correlation IDs must match exactly.

Edit MSMQ messages in a queue

I need to be able to edit messages in my error queue (so that they can be resent to the actual queue for reprocessing).
I would like to make my own custom tool to do this (because my messages require specific formatting to make them easily readable by support personnel).
I know that this is possible because the application "QueueExplorer" does this.
Does anyone know how I can load an MSMQ message (that is not the first one in the queue), edit it, and save it back to the queue?
Iterate through the messages, using something like this:
List<Message> msgList = new List<Message>();
using (MessageEnumerator me = queue.GetMessageEnumerator2())
{
while (me.MoveNext(new TimeSpan(0, 0, 0)))
{
Message message = me.Current;
msgList.Add(message)
}
}
You can then iterate through the list, processing each message.
Create a new message, based on the original. Then remove the existing message, and add the new one.
foreach (Message message in msgList)
{
//Create a new message as required, add it, then remove the old message
MessageQueue.ReceiveById(message.MessageId);
}
MSMQ messages are supposed to be immutable. The best you can do is read the message and send an edited copy of the message back to the queue.

Categories

Resources