PircBot API Java 字符串拆分问题
我需要一些关于管理员数组和其他东西的帮助。我正在使用这个 API 来制作服务器机器人。您可以在那里找到所有的类、构造函数和方法。
这是我当前的代码:
import org.jibble.pircbot.*;
public class MyBot extends PircBot {
public MyBot() {
this.setName("DevilBot");
}
String owner = "Evan";
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase("!time")) {
String time = new java.util.Date().toString();
sendMessage(channel, sender + ": The time is now " + time);
}
if (message.equalsIgnoreCase("!owner")) {
if(sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + "You're my owner silly!");
}
if (!sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + sender + ": " + owner + " is my owner!");
}
}
if (message.equalsIgnoreCase("!ban")) {
if(sender.equals(owner))
{
ban(channel, message);
sendMessage(channel, "Banned " + message);
}
else
{
kick(channel, sender);
sendMessage(channel, "You aren't my mother!");
}
}
if (message.equalsIgnoreCase("!version")) {
sendMessage(channel, "Version 0.1");
sendMessage(channel, "PircBot API v1.5.0");
}
if (message.equalsIgnoreCase("!aelux")) {
sendMessage(channel, "ALL HAIL AELUX!");
}
if (message.equalsIgnoreCase("!hates")){
sendMessage(channel, message + ", " + sender + " hates you!");
}
if (message.equalsIgnoreCase("!op")){
if(sender.equals(owner))
{
sendMessage(channel, "Opping " + message);
}
else
{
ban(channel, sender);
kick(channel, sender);
sendMessage(channel, "GTFO! Banned.");
}
}
}
}
它编译并运行良好。它仍处于 Alpha 阶段,但由于某种原因它不会读取我的命令。喜欢: !踢出用户 没有任何反应。
该 API 快速且易于理解。如果你能帮助我,那就太好了!
I need some help with arrays for admins and stuff. I'm using this API to make a server bot. You can find all the classes, constructors, and methods there.
Here's my current code:
import org.jibble.pircbot.*;
public class MyBot extends PircBot {
public MyBot() {
this.setName("DevilBot");
}
String owner = "Evan";
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase("!time")) {
String time = new java.util.Date().toString();
sendMessage(channel, sender + ": The time is now " + time);
}
if (message.equalsIgnoreCase("!owner")) {
if(sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + "You're my owner silly!");
}
if (!sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + sender + ": " + owner + " is my owner!");
}
}
if (message.equalsIgnoreCase("!ban")) {
if(sender.equals(owner))
{
ban(channel, message);
sendMessage(channel, "Banned " + message);
}
else
{
kick(channel, sender);
sendMessage(channel, "You aren't my mother!");
}
}
if (message.equalsIgnoreCase("!version")) {
sendMessage(channel, "Version 0.1");
sendMessage(channel, "PircBot API v1.5.0");
}
if (message.equalsIgnoreCase("!aelux")) {
sendMessage(channel, "ALL HAIL AELUX!");
}
if (message.equalsIgnoreCase("!hates")){
sendMessage(channel, message + ", " + sender + " hates you!");
}
if (message.equalsIgnoreCase("!op")){
if(sender.equals(owner))
{
sendMessage(channel, "Opping " + message);
}
else
{
ban(channel, sender);
kick(channel, sender);
sendMessage(channel, "GTFO! Banned.");
}
}
}
}
It compiles and runs fine. And it's still in Alpha Stages, but for some reason it won't read my command. Like:
!kick user
Yields no response.
The API is quick and easy to understand. If you CAN help me, that would be awesome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在在您的代码中您仅使用 equalsIgnoreCase
此方法要求两个字符串除了大小写外都完全匹配。
所以“!ban user”不起作用的原因如下。
但是
下面的这段代码将导致您能够通过输入“!禁止用户名”来禁止人们。但它会区分大小写。
如果您不希望它被分割,只需在分隔符(通常是空格)上分割传入的命令,然后转换第一个字符串并使用 equalsIgnoreCase 进行匹配。
希望这有帮助
Right now in your code you are only using equalsIgnoreCase
This method requires both strings to match perfectly apart from the case.
So the reason "!ban user" would not work is because of the following.
but
This following piece of code would result in you being able to ban people by typing "!ban username". However it will be case sensitive.
If you don't want it to be just split the incoming command on a separator (usually space), and convert the first string and match them with equalsIgnoreCase.
Hope this helps