Discord Bot spam reply issue - discord

public class PingPong extends ListenerAdapter {
public void onMessageReceived (MessageReceivedEvent ereignis) {
if (ereignis.isFromGuild()) {
if (ereignis.getMessage().getContentStripped().equals("ping"));
{
ereignis.getChannel().sendMessage("pong!").queue();
}
}
}
I tried to programm a ping and pong reply game where i type ping and the bot replys pong, but when i type ping it spams pong.
Edit: I found the Problem, it responds to every Message sent not only ping, but i still dont know why.

Related

How to fix discord jda bot spam

I'm working on a Discord bot but when I try to send a message to a channel, it doesn't work.
I tried to use the code event.getChannel().sendMessage("Hello!").queue(); But this one spams my discord.
public void onGuildMessageReceived (GuildMessageReceivedEvent event){
if(event.getChannel().getId().equalsIgnoreCase("607560497083973632")){
event.getChannel().sendMessage("Hello!").queue();
}
}
This is what happens: https://prntscr.com/oo6622
You receive your own messages. To prevent this you can just check if the received message is from the current user.
#Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event){
if (event.getAuthor().equals(event.getJDA().getSelfUser())) return; // ignore own messages
if (event.getChannel().getIdLong() == 607560497083973632L){ // use long for ids
event.getChannel().sendMessage("Hello!").queue();
}
}

Can async subscriber example lose messages?

Starting off with pubsub. When reading the google cloud documentation, i ran into a snippet of code, and i think i see a flaw with the example.
This is the code i am talking about. It uses the async subscriber.
public class SubscriberExample {
private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId();
private static final BlockingQueue<PubsubMessage> messages = new LinkedBlockingDeque<>();
static class MessageReceiverExample implements MessageReceiver {
#Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
messages.offer(message);
consumer.ack();
}
}
public static void main(String... args) throws Exception {
String subscriptionId = args[0];
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(
PROJECT_ID, subscriptionId);
Subscriber subscriber = null;
try {
subscriber =
Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build();
subscriber.startAsync().awaitRunning();
while (true) {
PubsubMessage message = messages.take();
processMessage(message);
}
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
My question is, what if a bunch of messages have been acknowledged, and the BlockingQueue is not empty, and the server crashes. Then i would lose some messages right? (Acknowledged in PubSub, but not actually processed).
Wouldn't the best implementation be to only acknowledge the message after the it has been processed? Instead of acknowledging it and leaving it on a queue, and assuming it will be processed. I understand this will decouple the receiving of messages and process of messages, and potentially increase throughput, but still it risks losing messages right?
Yes, one should not acknowledge a message until it has been fully processed. Otherwise, the message may never be processed because it will not be redelivered in the event of a crash or restart if it was acknowledged. I have entered an issue to update the example.

SignalR not working when using sql server as backlplane

I've tried to setup the following configuration with SQL Server and SignalR
http://www.asp.net/signalr/overview/performance/scaleout-with-sql-server
Everything seems to be setup correctly, if I profile SQL server I can see SignalR making calls to the DB, but when I call the hub to send a message to all the clients connected, the message is never sent to the connected browsers.
any idea what could be the problem?
Thank you #smnbss, you just saved my life with your comment inside your question. Just to make it clear for everyone in the future with the same problem, here is the wrong implementation: I was getting the context only once like:
public class SyncService : ISyncService
{
private IHubContext StatusChangeHub { get; set; }
public GatewaySyncService()
{
StatusChangeHub = GlobalHost.ConnectionManager.GetHubContext<Hub>();
}
public void SyncStatusChange(Status newStatus)
{
StatusChangeHub.Clients.All.onStatusChange(newStatus);
}
}
But somehow this only work while not using a backplane. And the correct implementation: you need to get the context everytime you want to send a message:
public class SyncService : ISyncService
{
public void SyncStatusChange(Status newStatus)
{
var context = GlobalHost.ConnectionManager.GetHubContext<Hub>();
context.Clients.All.onStatusChange(newStatus);
}
}

GAE Channel Sending First Message Repetatively

So this was working on this project a few months ago. I'm using Google App Engine's Channel API to push messages to my GWT app. I'm using http://code.google.com/p/gwt-gae-channel/ to interact through GWT.
Lets say I send 1 message to the client: "First Message"
The client will receive the message, "First Message" just fine.
Then let's say I send another message, "Second Message" to the client.
The client will again receive the message, "First Message".
This will continue happening. There have been some instances where I'll receive the second message, and it will be the message that gets stuck repeating.
When I finally close the page, and thus close the channel, I again receive the repeated message without sending something from the server.
Does anyone have any idea what is going on? I don't think this was happening when I was working on this a few months ago, and I can see no changes to the GAE Channel API.
Here is some code:
String json = AutoBeanHelper.toJson(proxy);
log.fine("Item's JSON Received: " + json);
List<ChannelEntity> channels = channelDAO.getByUserId();
if (channels.size() > 1) {
log.warning("Multiple channels for single user detected.");
}
ChannelService channelService = ChannelServiceFactory.getChannelService();
for (ChannelEntity channel : channels) {
channelService.sendMessage(new ChannelMessage(channel.getClientId(), json));
}
So whenever I store a new item of a specific type (this is in that entities update function):
1. I turn it into JSON.
2. I then log that JSON.
3. I get that users channel.
4. I send it to that users channel.
When I look at my logs, I see that the variable above that I'm logging is showing correctly, meaning I'm logging the correct JSON message but when I display the JSON in an alert on the client-side as soon as it gets to the client, it's the previous message that seems to be stuck repeating. I really don't see what I could be doing wrong here.
Let me know if you would like to see another part of the code. For good measure, here is the code on the client:
eventBus.addHandler(ReceivedChannelTokenEvent.TYPE, new ReceivedChannelTokenEventHandler() {
#Override
public void onEvent(ReceivedChannelTokenEvent event) {
ChannelFactory.createChannel(event.getChannelToken(), new ChannelCreatedCallback() {
#Override
public void onChannelCreated(Channel channel) {
final Socket channelSocket = channel.open(new SocketListener() {
#Override
public void onOpen() {
Window.alert("Channel Opened");
}
#Override
public void onMessage(String json) {
Window.alert(json);
eventBus.fireEvent(new MessageReceivedEvent(json));
}
#Override
public void onError(SocketError error) {
Window.alert("Channel Error: " + error.getDescription());
if ( error.getDescription().equals(CHANNEL_ERROR_TOKEN_TIME_OUT) ) {
eventBus.fireEvent(new ChannelTimeOutEvent());
}
}
#Override
public void onClose() {
Window.alert("Channel Closed.");
}
});
Window.addWindowClosingHandler(new Window.ClosingHandler() {
#Override
public void onWindowClosing(ClosingEvent event) {
channelSocket.close();
}
});
}
});
}
});
I see a lot of questions on SO where someone has a bug in their code but think it's part of the framework. Without seeing any code, I suspect there's some bug where you think you're sending "Second Message", but you're really sending a cached version of "First Message".
So I was finally able to figure it out. It seems that in the onMessage function within the app when I call
eventBus.fireEvent(new MessageReceivedEvent(json));
it seems that it never returns from this, thus never exiting the onMessage function in the code, causing me to receive the same message repetitively.

Google Talk receiving messages with different jid resource

I think I'm doing something wrong.
I want to send a XMPP message to my GTalk id but I don't want that the GTalk app receives the message so I'm changing the resource of the recipient JID.
My problem is that GTalk is receiving all the messages although thay have different resource.
My code:
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
JID jid = msg.getFromJid();
String body = msg.getBody();
String jidID = jid.getId().split("/")[0];
JID jid2 = new JID(jidID+"/myownresource453242352");
String response = jid2.getId() + " " + body;
// Send out response
msg = new MessageBuilder().withRecipientJids(jid2).withBody(response).build();
xmpp.sendMessage(msg);
}
The output:
Rafa Espillaque, 18:33 -
You shouldn't respond!
prueba-gae-gdx#appspot.com, 18:33 -
rafaespillaque#gmail.com/myownresource453242352 You shouldn't respond!
What's wrong?
UPDATE:
Now I'm sending messages to myapp#appspot.com/bot from an aSmack client and it is resending the message to me at my client.
The problem is GTalk for Gmail and GTalk for Android is registering all sent messages but they don't receive the app responses. Other clients don't show the messages I don't sent with them.
Will I be able to hide my messages to Gmail and Android?
My code:
SERVER
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException {
LOG.setLevel(Level.INFO);
// Parse incoming message
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message msg = xmpp.parseMessage(req);
LOG.info(msg.getStanza());
JID jid = msg.getFromJid();
String body = msg.getBody();
String response = "FullID: "+jid.getId()+" El mensaje recibido es: "+body;
// Send out response
msg = new MessageBuilder().
withRecipientJids(jid)
.withMessageType(MessageType.NORMAL)
.withBody(response)
.build();
xmpp.sendMessage(msg);
}
CLIENT:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
XMPPConnection connection = new XMPPConnection(connectionConfiguration);
try {
Log.i("TAG","Trying to connect");
connection.connect();
Log.i("TAG","Connected");
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
Log.i("TAG","Trying to Log In");
connection.login("rafaespillaque#gmail.com",mypass, mires");
Log.i("TAG","Logged In");
} catch (XMPPException e) {
e.printStackTrace();
Log.i("TAG","Problem connecting or logging in");
}
//Creating chat object for processing friend chat
Chat chat = connection.getChatManager().createChat(Server, new MessageListener() {
//Overriding process message function of MessageListener Interface which will be
//called whenever a message is received
#Override
public void processMessage(Chat c, Message m) {
//Displaying message sent by friend
//System.out.println(friendId+ " : " + m.getBody());
Log.i("TAG", m.getBody());
message = m.getBody();
}
});
try {
Message out = new Message();
out.setBody("Definitivo22222222");
out.setType(Type.normal);
chat.sendMessage(out);
Log.i("TAG", "Mensaje enviado");
} catch (XMPPException e) {
Log.i("TAG", "No se envió el mensaje");
e.printStackTrace();
}
Last thing: I've seen in AppEngine Logs that the Stanza received from aSmack isn't of normal type but chat type.
Thanks for helping!!
Last-last thing: You can test what Gmail is doing by connecting from any client and Gmail at same time and talking from the client. Gmail is receiving your messages.
Thanks again.
Another thing:
My goalis use XMPP to communicate 2 clients of a game with their gmail account. Do you know an alternative?
See RFC 6120, section 10.5.4:
If the JID contained in the 'to' attribute is of the form
localpart#domainpart/resourcepart and the user exists but there is
no connected resource that exactly matches the full JID, the stanza
SHOULD be processed as if the JID were of the form
localpart#domainpart as described under Section 10.5.3.2.
If you send to an invalid resource, the server treats it as if you had sent it to the bare JID. On GoogleTalk, this goes to all non-negative priority resources.
I think that this is by design. IIRC GTalk routes all messages for a given JID to all connected resources of the JID. This is even true if the message has a full JID as to.
If you send the message with JID and resource (user#yoursever/reourcex) then the receiving server will route your request to the intended receiver. The receiving server will decide how to route the JID/resource.
A standard server will look for an exact match of the sent JID/resource. If found then it will behave as expected and the message will be delivered. If that fails on the other hand, it will send the message to the highest priority online resource. If that fails, it will store the message offline.
In the case of Google Talk this will happen: it will try to match the exact resource, and if that fails, broadcast it to all of the online resources. The same is also applied if no resource included.

Resources