changeset 2:a2285ee1c687

Transformation legere de la facon dont les commandes sont gerees au niveau de Rusard
author defr@localhost.localdomain
date Sat, 12 May 2007 22:29:45 +0200
parents 4f934dfaa45a
children 568c206089c1
files org/defr/bots/Rusard.java
diffstat 1 files changed, 43 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/org/defr/bots/Rusard.java	Mon Apr 30 15:16:07 2007 +0200
+++ b/org/defr/bots/Rusard.java	Sat May 12 22:29:45 2007 +0200
@@ -7,51 +7,61 @@
 import java.util.Locale;
 
 public class Rusard extends PircBot {
-    
-    Random random;
 
-    public Rusard() {
-	 this.setName("Rusard");
-         random = new Random();
-    }
+	Random random;
 
-    public static void main(String arg[]) throws Exception {
-	Locale.setDefault(Locale.FRANCE);
-	Rusard b = new Rusard();
-	b.setVerbose(false);
-	b.setName("Rusard");
-	b.setLogin("Rusard");
-	b.setVersion("Rusard - MP Bot v0.0.1");
-	b.setEncoding("iso8859-1");
-	b.connect("hermes.local");
-	b.joinChannel("#quidditch");
-    }
+	public Rusard() {
+		this.setName("Rusard");
+		random = new Random();
+	}
 
-    public void onMessage(String ch, String s, String l, String h, String m) {
-    	messageParser(m, s, ch);
-    }
+	public static void main(String arg[]) throws Exception {
+		Locale.setDefault(Locale.FRANCE);
+		Rusard b = new Rusard();
+		b.setVerbose(false);
+		b.setName("Rusard");
+		b.setLogin("Rusard");
+		b.setVersion("Rusard - MP Bot v0.0.1");
+		b.setEncoding("iso8859-1");
+		b.connect("hermes.local");
+		b.joinChannel("#quidditch");
+	}
 
-    public void onPrivateMessage(String send, String login, String host, String m) {
-	messageParser("@" + m, send, send);
-    }
-    
-    public void messageParser(String message, String orig, String replyTo) {
-		if(message.equalsIgnoreCase("@time")) {
+	public void onMessage(String ch, String s, String l, String h, String m) {
+		if(m.startsWith("@") || m.startsWith(getNick() + ",")) {
+			String command;
+			if(m.startsWith("@")) {
+				command = m.substring(1);
+			}
+			else {
+				command = m.substring(getNick().length() + 1);
+				command = command.trim();
+			}
+			commandParser(command, s, ch);
+		}
+	}
+
+	public void onPrivateMessage(String send, String login, String host, String m) {
+		commandParser(m, send, send);
+	}
+
+	public void commandParser(String message, String orig, String replyTo) {
+		if(message.equalsIgnoreCase("time")) {
 			Calendar c = Calendar.getInstance(Locale.FRANCE);
 			DateFormat df = DateFormat.getInstance();
 			String t = df.format(c.getTime());
 			sendMessage(replyTo, "Nous sommes le " + t);
 		}
-		else if(message.equalsIgnoreCase("@de")) {
+		else if(message.equalsIgnoreCase("de")) {
 			int v = random.nextInt(6) + 1;
 			sendMessage(replyTo, "De : " + v);
 		}
-		else if(message.matches("^@d\\d+")) {
+		else if(message.matches("d\\d+")) {
 			int t = Integer.parseInt(message.substring(2));
 			int v = random.nextInt(t) + 1;
 			sendMessage(replyTo, "d" + t + " : " + v);
 		}
-		else if(message.matches("^@dd\\d")) {
+		else if(message.matches("dd\\d")) {
 			int t = Integer.parseInt(message.substring(3));
 			int limite = 0, score = random.nextInt(100) + 1;
 			switch(t) {
@@ -80,9 +90,9 @@
 				sendMessage(replyTo, "Echec (" + score + "/" + limite + ")");
 			}
 		}
-    }
+	}
 
-    public void onVersion(String sourceNick, String sourceLogin, String sourceHostname, String target) {
-	sendNotice(sourceNick, "Rusard - MP Bot v0.0.1");
-    }
+	public void onVersion(String sourceNick, String sourceLogin, String sourceHostname, String target) {
+		sendNotice(sourceNick, "Rusard - MP Bot v0.0.1");
+	}
 }