Coffee/Sample Extension

From Botdom Wiki
Jump to navigation Jump to search

The code below is the current version of the Away extension for Coffee.

Some things to note are:

  • Commands are implemented as events by the CommandSystem extension. Commands are registered with the prefix command: in the event type. ie. "command:away" represents !away.
  • The c argument of many of the below functions is the main "Config" object used by the bot. It allows extensions to share data, but only the object that adds a key may remove it.
    • The CommandSystem extension shares the "Trigger" used for command detection, for example.

<java> package plugins; import java.util.HashMap;

import dAmn.Packet; import dAmn.Writer;

import extensions.Config; import extensions.Manager;


public class AwayMessages {

private HashMap<String, AwayStatus> awayStatuses;

public AwayMessages(Manager eM, Config<String, String> c) { eM.registerExtension(this, "Away Messages", 1.0f, "CombatFrogs", "Simple away statuses for those without userscripts."); eM.registerEvent(this, "recv:msg", "recv_msg"); eM.registerEvent(this, "command:away", "cmd_away"); eM.registerEvent(this, "command:back", "cmd_back");

awayStatuses = new HashMap<String, AwayStatus>(); }

public void recv_msg(Packet p, Writer w, Config<String, String> c) { Packet sub = new Packet(p.getBody()); String message = sub.getBody().toLowerCase();

for(String username : awayStatuses.keySet()) { if(message.indexOf(username.toLowerCase()) == 0 && !sub.getArgument("from").toLowerCase().equals(c.get("Username").toLowerCase())) { if(System.currentTimeMillis() - awayStatuses.get(username).getLastAlert() >= 15000) { String out = new StringBuffer(sub.getArgument("from")).append(": ").append(username).append(" is away (").append(awayStatuses.get(username).getReason()).append(")").toString(); int elapsedTime = (int) ((System.currentTimeMillis() - awayStatuses.get(username).getAwaySince()) / 1000); w.sendMsg(p.getParameter(), out.replace("%TS%", elapsedTimeString(elapsedTime))); awayStatuses.get(username).updateAlert(); } } }

}

public void cmd_away(Packet p, Writer w, Config<String, String> c) { Packet sub = new Packet(p.getBody()); String from = sub.getArgument("from"); if(awayStatuses.keySet().contains(from)) { awayStatuses.remove(from); }

String reason = rebuildArgs(sub.getBody().split(" "), 1, -1, null).trim();

awayStatuses.put(from, new AwayStatus(reason));

w.sendMsg(p.getParameter(), new StringBuffer(from).append(" is now away. (").append(awayStatuses.get(from).getReason()).append(")").toString()); }

public void cmd_back(Packet p, Writer w, Config<String, String> c) { Packet sub = new Packet(p.getBody()); StringBuffer out = new StringBuffer(sub.getArgument("from")).append(": ");

if(awayStatuses.keySet().contains(sub.getArgument("from"))) { awayStatuses.remove(sub.getArgument("from")); out.append("You are now back."); } else out.append("You weren't away to start with.");

w.sendMsg(p.getParameter(), out.toString()); }

public String rebuildArgs(String[] argsArray, int start, int end, String sepChars) { if(end > argsArray.length - 1 || end < 0) end = argsArray.length - 1;

if(start > argsArray.length - 1) start = argsArray.length - 1;

if(start < 0) start = 0;

StringBuffer rebuild = new StringBuffer(""); for(int i = start; i <= end; i++) if(sepChars != null) rebuild.append(argsArray[i]).append(sepChars).append(" "); else rebuild.append(argsArray[i]).append(" ");

return rebuild.toString(); }

public String elapsedTimeString(int seconds) { StringBuffer sb = new StringBuffer();

int hours = seconds / 3600; seconds = seconds % 3600; if(hours > 0) sb.append(hours).append(" hours, ");

int minutes = seconds / 60; seconds = seconds % 60; if(minutes > 0) sb.append(minutes).append(" minutes, ");

if(sb.length() > 0) sb.append("and ");

sb.append(seconds).append(" seconds"); return sb.toString(); }

private class AwayStatus { private String reason; private long lastAlert; private long awaySince;

public AwayStatus(String reason) { this.reason = reason; this.updateAlert(); this.awaySince = lastAlert; } public String getReason() { return reason; } public long getLastAlert() { return lastAlert; } public void updateAlert() { this.lastAlert = System.currentTimeMillis(); } public long getAwaySince() { return awaySince; }


} }

</java>