login about faq


Trying out Java. I am not good at Java at all. Just want a bot that responds to commands like "!r1" to then output "Rule 1, No spam"

Need multiple lines but me being not so good I made this:

import org.jibble.pircbot.*;

public class MyBot extends PircBot {

public MyBot() {
    this.setName("Bot");
}

public void onMessage(String channel, String sender,
                   String login, String hostname, String message) {
    if (message.equalsIgnoreCase("hi")) {
        sendMessage(channel, "Hello!");
    if (message.equalsIgnoreCase("lol")) {
        sendMessage(channel, "LOL!");
    }
} } }

The first command works but anything after does not. It also can be used by anyone and should be op only.

Hope someone can give me a bit of advise :)

Jack,

asked Jun 18 '12 at 10:08

Jackster1337's gravatar image

Jackster1337
8.5k178213300


Its just a generic programming mistake. You have the second if inside the first if, meaning that you only check if the message is equal to "lol" if the message is equal to "hi", this just doesn't seem right ;) The right code would be:

public class MyBot extends PircBot {

public MyBot() {
    this.setName("Bot");
}

public void onMessage(String channel, String sender,
                   String login, String hostname, String message) {
    if (message.equalsIgnoreCase("hi")) {
        sendMessage(channel, "Hello!");
    }
    if (message.equalsIgnoreCase("lol")) {
        sendMessage(channel, "LOL!");
    }
} }

or:

public class MyBot extends PircBot {

public MyBot() {
    this.setName("Bot");
}

public void onMessage(String channel, String sender,
                   String login, String hostname, String message) {
    if (message.equalsIgnoreCase("hi")) {
        sendMessage(channel, "Hello!");
    } else if (message.equalsIgnoreCase("lol")) {
        sendMessage(channel, "LOL!");
    }
} }

So you don't check the string again even after you already found your match.

answered Jun 18 '12 at 20:13

hasta_la_vitoria's gravatar image

hasta_la_vitoria
162

edited Jun 18 '12 at 20:14

Thank you very much. Ill give that a go! Ill be learning Java and C next year so that will help :P

(Jun 19 '12 at 00:16) Jackster1337 Jackster1337's gravatar image
-1

You really think it's that easy to detect spamm? What your script can do for you without any problems is censoring curse words. But if you actually try to take away spam via this method then you will have a massive class with way to many if-else statements which will be extremely slow.

You can use regex to check for links and E-Mail address for spam at best but actually checking the message itself on everything in it is insane! You've added 'hi' to your list for 'Hello'. What if I were to say hey or 'what's up?' or even 'whats up' ? There are to many possibilities and after that you've got intended grammar mistakes like 'wahts up?' as if you've typed to fast.

As you said you have very little experience with Java yet you are trying to write a script that is a intermediate task even for the professional developers. My suggestion would be to talk to a developer you know and get some help on this.

answered Jun 18 '12 at 15:14

nitrocrime's gravatar image

nitrocrime
3.6k6277125

If you read the question and the script you can see that all it does is say something. Nothing to get rid of spam simply a way to warn people. But no you are trying to be a smart ass and has gotten it all wrong trying to make me look stupid and in turn has made himself look like a fool.

(Jun 19 '12 at 00:15) Jackster1337 Jackster1337's gravatar image

So "Just want a bot that responds to commands like "!r1" to then output "Rule 1, No spam"" doesn't mean you want to prevent spam? I'm confused..

@hasta_la_vitoria (cool name) made a good improvement but I stay with what I said, if you are going to do this in this way you will get a massive list of words to check through if you want to make it feel good to the user.

And me making you look like a fool? Yeah pretty much, wasn't my intention though. Oh and "But no you are trying to be a smart ass and has gotten it all wrong trying to make me look stupid and in turn has made himself look like a fool." Am I making myself look like a fool for outsmarting you is is this "himself" someone else?

(Jun 19 '12 at 04:18) nitrocrime nitrocrime's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported


Join Us in the Chat Room

Tags:

×61
×42
×1

Asked: Jun 18 '12 at 10:08

Seen: 414 times

Last updated: Jun 19 '12 at 04:18