parent
e2e8a58c4e
commit
eeb1955ebe
13 changed files with 145 additions and 148 deletions
7
pom.xml
7
pom.xml
|
@ -174,7 +174,7 @@
|
|||
<dependency>
|
||||
<groupId>com.discord4j</groupId>
|
||||
<artifactId>discord4j-core</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<version>3.0.3</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
|
||||
<dependency>
|
||||
|
@ -200,11 +200,6 @@
|
|||
<version>2.13.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.xaanit</groupId>
|
||||
<artifactId>D4J-OAuth</artifactId>
|
||||
<version>master-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
|
@ -58,7 +58,10 @@ public final class DPUtils {
|
|||
|
||||
public static ConfigData<MessageChannel> channelData(IHaveConfig config, String key, long defID) {
|
||||
return config.getDataPrimDef(key, defID, id -> {
|
||||
Channel ch = DiscordPlugin.dc.getChannelById(Snowflake.of((long) id)).block();
|
||||
Channel ch = DiscordPlugin.dc.getChannelById(Snowflake.of((long) id)).onErrorResume(e -> {
|
||||
getLogger().warning("Failed to get channel data for " + key + "=" + id + " - " + e.getMessage());
|
||||
return Mono.empty();
|
||||
}).block();
|
||||
if (ch instanceof MessageChannel)
|
||||
return (MessageChannel) ch;
|
||||
else
|
||||
|
@ -127,4 +130,8 @@ public final class DPUtils {
|
|||
? original.getAuthor().get().getMention() + ", " : "") + message));
|
||||
}
|
||||
|
||||
public static String nickMention(Snowflake userId) {
|
||||
return "<@!" + userId.asString() + ">";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import buttondevteam.lib.player.ChromaGamerBase;
|
|||
import com.google.common.io.Files;
|
||||
import discord4j.core.DiscordClient;
|
||||
import discord4j.core.DiscordClientBuilder;
|
||||
import discord4j.core.event.domain.guild.GuildCreateEvent;
|
||||
import discord4j.core.event.domain.lifecycle.ReadyEvent;
|
||||
import discord4j.core.object.entity.Guild;
|
||||
import discord4j.core.object.entity.MessageChannel;
|
||||
|
@ -35,14 +36,14 @@ import org.bukkit.Bukkit;
|
|||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DiscordPlugin extends ButtonPlugin {
|
||||
|
@ -105,15 +106,16 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
}
|
||||
}
|
||||
val cb = new DiscordClientBuilder(token);
|
||||
cb.setInitialPresence(Presence.doNotDisturb(Activity.playing("booting")));
|
||||
dc = cb.build();
|
||||
dc.getEventDispatcher().on(ReadyEvent.class).subscribe(this::handleReady);
|
||||
/*dc.getEventDispatcher().on(ReadyEvent.class) // Listen for ReadyEvent(s)
|
||||
dc.getEventDispatcher().on(ReadyEvent.class) // Listen for ReadyEvent(s)
|
||||
.map(event -> event.getGuilds().size()) // Get how many guilds the bot is in
|
||||
.flatMap(size -> dc.getEventDispatcher()
|
||||
.on(GuildCreateEvent.class) // Listen for GuildCreateEvent(s)
|
||||
.take(size) // Take only the first `size` GuildCreateEvent(s) to be received
|
||||
.collectList()) // Take all received GuildCreateEvents and make it a List
|
||||
.subscribe(events -> /* All guilds have been received, client is fully connected *);*/ //TODO
|
||||
.subscribe(this::handleReady); /* All guilds have been received, client is fully connected */
|
||||
dc.login().subscribe();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
|
@ -122,41 +124,25 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
|
||||
public static Guild mainServer;
|
||||
|
||||
private static volatile BukkitTask task;
|
||||
private static volatile boolean sent = false;
|
||||
|
||||
private void handleReady(ReadyEvent event) {
|
||||
private void handleReady(List<GuildCreateEvent> event) {
|
||||
try {
|
||||
dc.updatePresence(Presence.doNotDisturb(Activity.playing("booting"))).subscribe();
|
||||
val tries = new AtomicInteger();
|
||||
task = Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
|
||||
tries.incrementAndGet();
|
||||
if (tries.get() > 10) { //5 seconds
|
||||
task.cancel();
|
||||
getLogger().severe("Main server not found! Invite the bot and do /discord reset");
|
||||
//getIConfig().getConfig().set("mainServer", 219529124321034241L); //Needed because it won't save as long as it's null - made it save
|
||||
saveConfig(); //Put default there
|
||||
return;
|
||||
}
|
||||
mainServer = MainServer().get(); //Shouldn't change afterwards
|
||||
if (mainServer == null) {
|
||||
val guilds = dc.getGuilds();
|
||||
if (guilds.count().blockOptional().orElse(0L) == 0L)
|
||||
return; //If there are no guilds in cache, retry
|
||||
mainServer = guilds.blockFirst();
|
||||
if (mainServer == null) return;
|
||||
if (event.size() == 0) {
|
||||
getLogger().severe("Main server not found! Invite the bot and do /discord reset");
|
||||
saveConfig(); //Put default there
|
||||
return; //We should have all guilds by now, no need to retry
|
||||
}
|
||||
mainServer = event.get(0).getGuild();
|
||||
getLogger().warning("Main server set to first one: " + mainServer.getName());
|
||||
MainServer().set(mainServer); //Save in config
|
||||
}
|
||||
if (!TBMCCoreAPI.IsTestServer()) { //Don't change conditions here, see mainServer=devServer=null in onDisable()
|
||||
dc.updatePresence(Presence.online(Activity.playing("Minecraft")));
|
||||
dc.updatePresence(Presence.online(Activity.playing("Minecraft"))).subscribe();
|
||||
} else {
|
||||
dc.updatePresence(Presence.online(Activity.playing("testing")));
|
||||
dc.updatePresence(Presence.online(Activity.playing("testing"))).subscribe();
|
||||
}
|
||||
SafeMode = false;
|
||||
if (task != null)
|
||||
task.cancel();
|
||||
if (!sent) {
|
||||
DPUtils.disableIfConfigError(null, CommandChannel(), ModRole()); //Won't disable, just prints the warning here
|
||||
|
||||
Component.registerComponent(this, new GeneralEventBroadcasterModule());
|
||||
|
@ -190,8 +176,7 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
|
||||
getConfig().set("serverup", true);
|
||||
saveConfig();
|
||||
sent = true;
|
||||
if (TBMCCoreAPI.IsTestServer() && !event.getSelf().getUsername().toLowerCase().contains("test")) {
|
||||
if (TBMCCoreAPI.IsTestServer() && !Objects.requireNonNull(dc.getSelf().block()).getUsername().toLowerCase().contains("test")) {
|
||||
TBMCCoreAPI.SendException(
|
||||
"Won't load because we're in testing mode and not using a separate account.",
|
||||
new Exception(
|
||||
|
@ -200,8 +185,7 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
}
|
||||
TBMCCoreAPI.SendUnsentExceptions();
|
||||
TBMCCoreAPI.SendUnsentDebugMessages();
|
||||
}
|
||||
}, 0, 10);
|
||||
|
||||
CommonListeners.register(dc.getEventDispatcher());
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
||||
getCommand2MC().registerCommand(new DiscordMCCommand());
|
||||
|
@ -253,7 +237,6 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
dc.updatePresence(Presence.idle(Activity.playing("Chromacraft"))).block(); //No longer using the same account for testing
|
||||
dc.logout().block();
|
||||
//Configs are emptied so channels and servers are fetched again
|
||||
sent = false;
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("An error occured while disabling DiscordPlugin!", e);
|
||||
}
|
||||
|
@ -263,7 +246,7 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
|
||||
public static Permission perms;
|
||||
|
||||
public boolean setupProviders() {
|
||||
private boolean setupProviders() {
|
||||
try {
|
||||
Class.forName("net.milkbowl.vault.permission.Permission");
|
||||
Class.forName("net.milkbowl.vault.chat.Chat");
|
||||
|
|
|
@ -58,7 +58,7 @@ public abstract class DiscordSenderBase implements CommandSender {
|
|||
msgtosend += "\n" + sendmsg;
|
||||
if (sendtask == null)
|
||||
sendtask = Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> {
|
||||
channel.createMessage((!broadcast && user != null ? user.getMention() + "\n" : "") + msgtosend.trim());
|
||||
channel.createMessage((!broadcast && user != null ? user.getMention() + "\n" : "") + msgtosend.trim()).subscribe();
|
||||
sendtask = null;
|
||||
msgtosend = "";
|
||||
}, 4); // Waits a 0.2 second to gather all/most of the different messages
|
||||
|
|
|
@ -18,9 +18,12 @@ public class Command2DCSender implements Command2Sender {
|
|||
message = DPUtils.sanitizeString(message);
|
||||
message = Character.toLowerCase(message.charAt(0)) + message.substring(1);
|
||||
val msg = message;
|
||||
val author = this.message.getAuthorAsMember().block();
|
||||
if (author == null) return;
|
||||
this.message.getChannel().subscribe(ch -> ch.createMessage(author.getNicknameMention() + ", " + msg));
|
||||
/*this.message.getAuthorAsMember().flatMap(author ->
|
||||
this.message.getChannel().flatMap(ch ->
|
||||
ch.createMessage(author.getNicknameMention() + ", " + msg))).subscribe();*/
|
||||
this.message.getChannel().flatMap(ch ->
|
||||
ch.createMessage(this.message.getAuthor().map(u -> DPUtils.nickMention(u.getId()) + ", ").orElse("")
|
||||
+ msg)).subscribe();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package buttondevteam.discordplugin.commands;
|
||||
|
||||
import buttondevteam.lib.chat.Command2;
|
||||
import buttondevteam.lib.chat.CommandClass;
|
||||
|
||||
@CommandClass(helpText = {
|
||||
|
@ -7,12 +8,17 @@ import buttondevteam.lib.chat.CommandClass;
|
|||
"Shows some info about a command or lists the available commands.", //
|
||||
})
|
||||
public class HelpCommand extends ICommand2DC {
|
||||
@Override
|
||||
public boolean def(Command2DCSender sender, String args) {
|
||||
if (args.length() == 0)
|
||||
@Command2.Subcommand
|
||||
public boolean def(Command2DCSender sender, @Command2.TextArg @Command2.OptionalArg String args) {
|
||||
if (args == null || args.length() == 0)
|
||||
sender.sendMessage(getManager().getCommandsText());
|
||||
else {
|
||||
String[] ht = getManager().getHelpText(args);
|
||||
if (ht == null)
|
||||
sender.sendMessage("Command not found: " + args);
|
||||
else
|
||||
sender.sendMessage("Soon:tm:"); //TODO
|
||||
sender.sendMessage(ht);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ public class CommandListener {
|
|||
if (!message.getContent().isPresent())
|
||||
return false; //Pin messages and such, let the mcchat listener deal with it
|
||||
final MessageChannel channel = message.getChannel().block();
|
||||
@SuppressWarnings("OptionalGetWithoutIsPresent") val content = message.getContent().get();
|
||||
val content = message.getContent().get();
|
||||
if (channel == null) return false;
|
||||
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
|
||||
if (!(channel instanceof PrivateChannel)
|
||||
|
@ -57,6 +57,7 @@ public class CommandListener {
|
|||
}
|
||||
|
||||
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, Message message) {
|
||||
final char prefix = DiscordPlugin.getPrefix();
|
||||
if (message.getContent().orElse("").startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
|
||||
if (cmdwithargs.length() > mention.length() + 1) {
|
||||
int i = cmdwithargs.indexOf(" ", mention.length());
|
||||
|
@ -67,14 +68,16 @@ public class CommandListener {
|
|||
for (; i < cmdwithargs.length() && cmdwithargs.charAt(i) == ' '; i++)
|
||||
; //Removes any space before the command
|
||||
cmdwithargs.delete(0, i);
|
||||
cmdwithargs.insert(0, DiscordPlugin.getPrefix()); //Always use the prefix for processing
|
||||
cmdwithargs.insert(0, prefix); //Always use the prefix for processing
|
||||
} else
|
||||
cmdwithargs.replace(0, cmdwithargs.length(), DiscordPlugin.getPrefix() + "help");
|
||||
cmdwithargs.replace(0, cmdwithargs.length(), prefix + "help");
|
||||
else {
|
||||
if (cmdwithargs.length() == 0)
|
||||
cmdwithargs.replace(0, cmdwithargs.length(), prefix + "help");
|
||||
else if (cmdwithargs.charAt(0) != prefix)
|
||||
cmdwithargs.insert(0, prefix);
|
||||
return false; //Don't treat / as mention, mentions can be used in public mcchat
|
||||
}
|
||||
if (cmdwithargs.length() == 0)
|
||||
cmdwithargs.replace(0, cmdwithargs.length(), DiscordPlugin.getPrefix() + "help");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@ public class CommonListeners {
|
|||
val author = event.getMessage().getAuthor();
|
||||
if (!author.isPresent() || author.get().isBot())
|
||||
return;
|
||||
//System.out.println("Author: "+author.get());
|
||||
//System.out.println("Bot: "+author.get().isBot());
|
||||
if (FunModule.executeMemes(event.getMessage()))
|
||||
return;
|
||||
try {
|
||||
|
@ -42,6 +44,7 @@ public class CommonListeners {
|
|||
|| event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels
|
||||
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
|
||||
if (handled) return;
|
||||
//System.out.println("Message handling");
|
||||
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
|
||||
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
|
||||
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
|
||||
|
|
|
@ -5,7 +5,6 @@ import buttondevteam.core.component.channel.ChatRoom;
|
|||
import buttondevteam.discordplugin.DiscordConnectedPlayer;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import discord4j.core.object.entity.MessageChannel;
|
||||
import discord4j.core.object.entity.TextChannel;
|
||||
import discord4j.core.object.entity.User;
|
||||
import discord4j.core.object.util.Snowflake;
|
||||
import lombok.NonNull;
|
||||
|
@ -65,7 +64,7 @@ public class MCChatCustom {
|
|||
|
||||
private CustomLMD(@NonNull MessageChannel channel, @NonNull User user,
|
||||
@NonNull String groupid, @NonNull Channel mcchannel, @NonNull DiscordConnectedPlayer dcp, int toggles, Set<TBMCSystemChatEvent.BroadcastTarget> brtoggles) {
|
||||
super((TextChannel) channel, user);
|
||||
super(channel, user);
|
||||
groupID = groupid;
|
||||
this.mcchannel = mcchannel;
|
||||
this.dcp = dcp;
|
||||
|
|
|
@ -236,6 +236,7 @@ public class MCChatListener implements Listener {
|
|||
if (CommandListener.runCommand(ev.getMessage(), true))
|
||||
return true; //Allow running commands in chat channels
|
||||
MCChatUtils.resetLastMessage(channel);
|
||||
//System.out.println("Message: "+ev.getMessage().getAuthor().toString());
|
||||
recevents.add(ev);
|
||||
if (rectask != null)
|
||||
return true;
|
||||
|
|
|
@ -7,7 +7,6 @@ import buttondevteam.discordplugin.DiscordPlugin;
|
|||
import buttondevteam.lib.player.TBMCPlayer;
|
||||
import discord4j.core.object.entity.MessageChannel;
|
||||
import discord4j.core.object.entity.PrivateChannel;
|
||||
import discord4j.core.object.entity.TextChannel;
|
||||
import discord4j.core.object.entity.User;
|
||||
import lombok.val;
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -44,7 +43,7 @@ public class MCChatPrivate {
|
|||
if (!start)
|
||||
MCChatUtils.lastmsgfromd.remove(channel.getId().asLong());
|
||||
return start //
|
||||
? lastmsgPerUser.add(new MCChatUtils.LastMsgData((TextChannel) channel, user)) // Doesn't support group DMs
|
||||
? lastmsgPerUser.add(new MCChatUtils.LastMsgData(channel, user)) // Doesn't support group DMs
|
||||
: lastmsgPerUser.removeIf(lmd -> lmd.channel.getId().asLong() == channel.getId().asLong());
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@ import buttondevteam.lib.TBMCSystemChatEvent;
|
|||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
import com.google.common.collect.Lists;
|
||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||
import discord4j.core.object.entity.MessageChannel;
|
||||
import discord4j.core.object.util.Snowflake;
|
||||
import lombok.Getter;
|
||||
|
@ -74,7 +73,6 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
|
|||
protected void enable() {
|
||||
if (DPUtils.disableIfConfigError(this, chatChannel())) return;
|
||||
listener = new MCChatListener(this);
|
||||
DiscordPlugin.dc.getEventDispatcher().on(MessageCreateEvent.class).subscribe(listener::handleDiscord);
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin());
|
||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(this), getPlugin());//These get undone if restarting/resetting - it will ignore events if disabled
|
||||
getPlugin().getManager().registerCommand(new MCChatCommand());
|
||||
|
|
|
@ -104,9 +104,9 @@ public class DiscordMCCommand extends ICommand2MC {
|
|||
}
|
||||
DiscordPlugin.mainServer.getInvites().limitRequest(1)
|
||||
.switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("§cNo invites found for the server.")))
|
||||
.subscribe(inv -> {//TODO: Needs manage server perms
|
||||
.subscribe(inv -> {
|
||||
sender.sendMessage("§bInvite link: https://discord.gg/" + inv.getCode());
|
||||
});
|
||||
}, e -> sender.sendMessage("§cThe invite link is not set and the bot has no permission to get it."));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue