use of com.denizenscript.ddiscordbot.DiscordConnection in project dDiscordBot by DenizenScript.
the class DiscordCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag instruction = scriptEntry.getElement("instruction");
// Intentionally do not debug this value.
ElementTag code = scriptEntry.getElement("code");
ElementTag tokenFile = scriptEntry.getElement("tokenfile");
DiscordChannelTag channel = scriptEntry.getObjectTag("channel");
ElementTag message = scriptEntry.getElement("message");
ElementTag status = scriptEntry.getElement("status");
ElementTag activity = scriptEntry.getElement("activity");
DiscordUserTag user = scriptEntry.getObjectTag("user");
DiscordGroupTag guild = scriptEntry.getObjectTag("group");
DiscordRoleTag role = scriptEntry.getObjectTag("role");
ElementTag url = scriptEntry.getElement("url");
ElementTag messageId = scriptEntry.getElement("message_id");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, channel, instruction, message, user, guild, role, status, activity, url, tokenFile, messageId);
}
Supplier<Boolean> requireClientID = () -> {
if (!DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
handleError(scriptEntry, "Failed to process Discord " + instruction.asString() + " command: unknown ID!");
scriptEntry.setFinished(true);
return true;
}
return false;
};
Function<JDA, Boolean> requireClientObject = (_client) -> {
if (_client == null) {
handleError(scriptEntry, "The Discord bot '" + id.asString() + "'is not yet loaded.");
scriptEntry.setFinished(true);
return true;
}
return false;
};
BiFunction<Object, String, Boolean> requireObject = (obj, name) -> {
if (obj == null) {
handleError(scriptEntry, "Failed to process Discord " + instruction.asString() + " command: no " + name + " given!");
scriptEntry.setFinished(true);
return true;
}
return false;
};
Supplier<Boolean> requireUser = () -> requireObject.apply(user, "user");
Supplier<Boolean> requireChannel = () -> requireObject.apply(channel, "channel");
Supplier<Boolean> requireMessage = () -> requireObject.apply(message, "message");
Supplier<Boolean> requireGuild = () -> requireObject.apply(guild, "guild");
Supplier<Boolean> requireRole = () -> requireObject.apply(role, "role");
Supplier<Boolean> requireMessageId = () -> requireObject.apply(messageId, "message_id");
DiscordInstruction instructionEnum = DiscordInstruction.valueOf(instruction.asString().toUpperCase());
if (instructionEnum == DiscordInstruction.CONNECT) {
if (code != null && scriptEntry.dbCallShouldDebug() && com.denizenscript.denizen.utilities.debugging.Debug.record) {
handleError(scriptEntry, "You almost recorded debug of your Discord token - record automatically disabled to protect you.");
com.denizenscript.denizen.utilities.debugging.Debug.record = false;
}
}
Runnable executeCore = () -> {
try {
switch(instructionEnum) {
case CONNECT:
{
DenizenDiscordBot.oldConnectCommand.warn(scriptEntry);
if (code == null && tokenFile == null) {
requireObject.apply(null, "tokenfile");
break;
}
if (DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
handleError(scriptEntry, "Failed to connect: duplicate ID!");
break;
}
String codeRaw;
if (code != null) {
codeRaw = code.asString();
} else {
File f = new File(Denizen.getInstance().getDataFolder(), tokenFile.asString());
if (!Utilities.canReadFile(f)) {
handleError(scriptEntry, "Cannot read from that token file path due to security settings in Denizen/config.yml.");
scriptEntry.setFinished(true);
break;
}
if (!f.exists()) {
handleError(scriptEntry, "Invalid tokenfile specified. File does not exist.");
scriptEntry.setFinished(true);
break;
}
codeRaw = CoreUtilities.journallingLoadFile(f.getAbsolutePath());
if (codeRaw == null || codeRaw.length() < 5 || codeRaw.length() > 200) {
handleError(scriptEntry, "Invalid tokenfile specified. File content doesn't look like a bot token.");
scriptEntry.setFinished(true);
break;
}
codeRaw = codeRaw.trim();
}
DiscordConnection dc = new DiscordConnection();
dc.botID = id.asString();
DenizenDiscordBot.instance.connections.put(id.asString(), dc);
DiscordConnectCommand.DiscordConnectThread dct = new DiscordConnectCommand.DiscordConnectThread();
dct.code = codeRaw;
dct.conn = dc;
dct.ender = () -> scriptEntry.setFinished(true);
dct.start();
break;
}
case DISCONNECT:
{
if (requireClientID.get()) {
return;
}
DiscordConnection dc = DenizenDiscordBot.instance.connections.remove(id.asString());
if (dc.flags.modified) {
dc.flags.saveToFile(DiscordConnectCommand.flagFilePathFor(id.asString()));
}
dc.client.shutdown();
scriptEntry.setFinished(true);
break;
}
case MESSAGE:
{
DenizenDiscordBot.oldMessageCommand.warn(scriptEntry);
if (channel == null && user == null) {
if (!requireChannel.get()) {
requireUser.get();
}
scriptEntry.setFinished(true);
return;
}
if (requireClientID.get() || requireMessage.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
MessageChannel textChan;
if (channel == null) {
User userObj = client.getUserById(user.user_id);
if (userObj == null) {
handleError(scriptEntry, "Invalid or unrecognized user (given user ID not valid? Have you enabled the 'members' intent?).");
scriptEntry.setFinished(true);
return;
}
textChan = userObj.openPrivateChannel().complete();
} else {
textChan = client.getTextChannelById(channel.channel_id);
}
if (textChan == null) {
handleError(scriptEntry, "No channel to send message to (channel ID invalid, or not a text channel?).");
scriptEntry.setFinished(true);
return;
}
Message sentMessage;
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
sentMessage = textChan.sendMessageEmbeds(embed).complete();
} else {
sentMessage = textChan.sendMessage(message.asString()).complete();
}
scriptEntry.addObject("message_id", new ElementTag(sentMessage.getId()));
scriptEntry.setFinished(true);
break;
}
case ADD_ROLE:
{
if (requireClientID.get() || requireUser.get() || requireGuild.get() || requireRole.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
Guild guildObj = client.getGuildById(guild.guild_id);
Member memberObj = guildObj.getMemberById(user.user_id);
guildObj.addRoleToMember(memberObj, guildObj.getRoleById(role.role_id)).complete();
scriptEntry.setFinished(true);
break;
}
case REMOVE_ROLE:
{
if (requireClientID.get() || requireUser.get() || requireRole.get() || requireGuild.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
Guild guildObj = client.getGuildById(guild.guild_id);
Member memberObj = guildObj.getMemberById(user.user_id);
guildObj.removeRoleFromMember(memberObj, guildObj.getRoleById(role.role_id)).complete();
scriptEntry.setFinished(true);
break;
}
case EDIT_MESSAGE:
{
DenizenDiscordBot.oldEditMessage.warn(scriptEntry);
if (requireClientID.get() || requireChannel.get() || requireMessage.get() || requireMessageId.get()) {
return;
}
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (requireClientObject.apply(connection == null ? null : connection.client)) {
return;
}
MessageChannel textChannel = (MessageChannel) connection.getChannel(channel.channel_id);
if (message.asString().startsWith("discordembed@")) {
MessageEmbed embed = DiscordEmbedTag.valueOf(message.asString(), scriptEntry.context).build(scriptEntry.context).build();
textChannel.editMessageEmbedsById(messageId.asLong(), embed).complete();
} else {
textChannel.editMessageById(messageId.asLong(), message.asString()).complete();
}
scriptEntry.setFinished(true);
break;
}
case DELETE_MESSAGE:
{
DenizenDiscordBot.oldDeleteMessage.warn(scriptEntry);
if (requireClientID.get() || requireChannel.get() || requireMessageId.get()) {
return;
}
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (requireClientObject.apply(connection == null ? null : connection.client)) {
return;
}
((MessageChannel) connection.getChannel(channel.channel_id)).deleteMessageById(messageId.asLong()).complete();
scriptEntry.setFinished(true);
break;
}
case START_TYPING:
{
if (requireClientID.get() || requireChannel.get()) {
return;
}
DiscordConnection connection = DenizenDiscordBot.instance.connections.get(id.asString());
if (requireClientObject.apply(connection == null ? null : connection.client)) {
return;
}
MessageChannel textChannel = (MessageChannel) connection.getChannel(channel.channel_id);
textChannel.sendTyping().complete();
scriptEntry.setFinished(true);
break;
}
case STOP_TYPING:
{
DenizenDiscordBot.oldStopTyping.warn(scriptEntry);
if (requireClientID.get() || requireChannel.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
// TODO: ?
scriptEntry.setFinished(true);
break;
}
case RENAME:
{
if (requireClientID.get() || requireGuild.get() || requireMessage.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
long userId;
if (user == null) {
userId = client.getSelfUser().getIdLong();
} else {
userId = user.user_id;
}
client.getGuildById(guild.guild_id).getMemberById(userId).modifyNickname(message.asString()).complete();
scriptEntry.setFinished(true);
break;
}
case STATUS:
{
if (requireClientID.get()) {
return;
}
JDA client = DenizenDiscordBot.instance.connections.get(id.asString()).client;
if (requireClientObject.apply(client)) {
return;
}
Activity at;
String activityType = CoreUtilities.toLowerCase(activity.toString());
switch(activityType) {
case "watching":
at = Activity.watching(message.asString());
break;
case "streaming":
at = Activity.streaming(message.asString(), url.asString());
break;
case "listening":
at = Activity.listening(message.asString());
break;
default:
at = Activity.playing(message.asString());
break;
}
String statusLower = status == null ? "online" : CoreUtilities.toLowerCase(status.asString());
OnlineStatus statusType;
switch(statusLower) {
case "idle":
statusType = OnlineStatus.IDLE;
break;
case "dnd":
statusType = OnlineStatus.DO_NOT_DISTURB;
break;
case "invisible":
statusType = OnlineStatus.INVISIBLE;
break;
default:
statusType = OnlineStatus.ONLINE;
break;
}
client.getPresence().setPresence(statusType, at);
scriptEntry.setFinished(true);
break;
}
}
} catch (Throwable ex) {
handleError(scriptEntry, ex);
scriptEntry.setFinished(true);
}
};
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, executeCore);
} else {
executeCore.run();
}
}
Aggregations