use of com.denizenscript.ddiscordbot.DiscordConnection in project dDiscordBot by DenizenScript.
the class DiscordConnectCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.requiredArgForPrefixAsElement("id");
ElementTag tokenFile = scriptEntry.argForPrefixAsElement("tokenfile", null);
SecretTag token = scriptEntry.argForPrefix("token", SecretTag.class, true);
ListTag intents = scriptEntry.argForPrefix("intents", ListTag.class, true);
if (tokenFile == null && token == null) {
throw new InvalidArgumentsRuntimeException("Missing token SecretTag object!");
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, token, tokenFile, intents);
}
if (DenizenDiscordBot.instance.connections.containsKey(id.asString())) {
Debug.echoError("Failed to connect: duplicate ID!");
return;
}
DiscordConnection dc = new DiscordConnection();
dc.botID = id.asString();
DenizenDiscordBot.instance.connections.put(id.asString(), dc);
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
String codeRaw;
if (tokenFile != null) {
DenizenDiscordBot.oldTokenFile.warn(scriptEntry);
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);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
if (!f.exists()) {
handleError(scriptEntry, "Invalid tokenfile specified. File does not exist.");
scriptEntry.setFinished(true);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
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);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
} else {
codeRaw = token.getValue();
}
codeRaw = codeRaw.trim();
DiscordConnectThread dct = new DiscordConnectThread();
dct.scriptEntry = scriptEntry;
dct.code = codeRaw;
dct.conn = dc;
dct.ender = () -> scriptEntry.setFinished(true);
if (intents != null) {
try {
for (String intent : intents) {
dct.intents.add(GatewayIntent.valueOf(intent.toUpperCase()));
}
} catch (IllegalArgumentException ex) {
Debug.echoError("Invalid 'intents' input - " + ex.getMessage());
scriptEntry.setFinished(true);
DenizenDiscordBot.instance.connections.remove(id.asString());
return;
}
}
dct.start();
});
}
use of com.denizenscript.ddiscordbot.DiscordConnection in project dDiscordBot by DenizenScript.
the class DiscordMessageCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) {
DiscordBotTag bot = scriptEntry.requiredArgForPrefix("id", DiscordBotTag.class);
DiscordChannelTag channel = scriptEntry.argForPrefix("channel", DiscordChannelTag.class, true);
ObjectTag message = scriptEntry.getObjectTag("message");
DiscordUserTag user = scriptEntry.argForPrefix("user", DiscordUserTag.class, true);
DiscordMessageTag reply = scriptEntry.argForPrefix("reply", DiscordMessageTag.class, true);
DiscordMessageTag edit = scriptEntry.argForPrefix("edit", DiscordMessageTag.class, true);
boolean noMention = scriptEntry.argAsBoolean("no_mention");
ElementTag attachFileName = scriptEntry.argForPrefixAsElement("attach_file_name", null);
ElementTag attachFileText = scriptEntry.argForPrefixAsElement("attach_file_text", null);
ObjectTag rows = scriptEntry.argForPrefix("rows", ObjectTag.class, true);
if (scriptEntry.dbCallShouldDebug()) {
// Note: attachFileText intentionally at end
Debug.report(scriptEntry, getName(), bot, channel, message, user, reply, noMention, rows, attachFileName, attachFileText);
}
if (message == null && attachFileName == null) {
throw new InvalidArgumentsRuntimeException("Must have a message!");
}
Runnable runner = () -> {
DiscordConnection connection = bot.getConnection();
JDA client = connection.client;
MessageChannel toChannel = null;
if (reply != null && reply.channel_id != 0) {
Channel result = connection.getChannel(reply.channel_id);
if (result instanceof MessageChannel) {
toChannel = (MessageChannel) result;
} else {
handleError(scriptEntry, "Invalid reply message channel ID given.");
return;
}
} else if (edit != null && edit.channel_id != 0) {
Channel result = connection.getChannel(edit.channel_id);
if (result instanceof MessageChannel) {
toChannel = (MessageChannel) result;
} else {
handleError(scriptEntry, "Invalid edit message channel ID given.");
return;
}
} else if (channel != null) {
Channel result = connection.getChannel(channel.channel_id);
if (result instanceof MessageChannel) {
toChannel = (MessageChannel) result;
} else {
handleError(scriptEntry, "Invalid channel ID given.");
return;
}
} else if (user != 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?).");
return;
}
toChannel = userObj.openPrivateChannel().complete();
}
if (toChannel == null) {
handleError(scriptEntry, "Failed to process DiscordMessage command: no channel given!");
return;
}
Message replyTo = null;
if (reply != null) {
replyTo = reply.bot != null ? reply.getMessage() : null;
if (replyTo == null) {
replyTo = toChannel.retrieveMessageById(reply.message_id).complete();
}
if (replyTo == null) {
handleError(scriptEntry, "Failed to process DiscordMessage reply: invalid message to reply to!");
return;
}
}
MessageAction action = null;
boolean isFile = false;
if (message == null || message.toString().length() == 0) {
if (attachFileName != null) {
if (attachFileText != null) {
if (reply != null) {
action = replyTo.reply(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
} else {
action = toChannel.sendFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
}
isFile = true;
}
}
} else if (message.shouldBeType(DiscordEmbedTag.class)) {
MessageEmbed embed = message.asType(DiscordEmbedTag.class, scriptEntry.context).build(scriptEntry.context).build();
if (reply != null) {
action = replyTo.replyEmbeds(embed);
} else if (edit != null) {
action = toChannel.editMessageEmbedsById(edit.message_id, embed);
} else {
action = toChannel.sendMessageEmbeds(embed);
}
} else {
if (reply != null) {
action = replyTo.reply(message.toString());
} else if (edit != null) {
action = toChannel.editMessageById(edit.message_id, message.toString());
} else {
action = toChannel.sendMessage(message.toString());
}
}
if (action == null) {
handleError(scriptEntry, "Failed to send message - missing content?");
return;
}
if (!isFile && attachFileName != null) {
if (attachFileText != null) {
action = action.addFile(attachFileText.asString().getBytes(StandardCharsets.UTF_8), attachFileName.asString());
} else {
handleError(scriptEntry, "Failed to send attachment - missing content?");
}
}
List<ActionRow> actionRows = createRows(scriptEntry, rows);
if (actionRows != null) {
action.setActionRows(actionRows);
}
if (noMention) {
action = action.mentionRepliedUser(false);
}
Message sentMessage = action.complete();
scriptEntry.addObject("message", new DiscordMessageTag(bot.bot, sentMessage));
};
if (scriptEntry.shouldWaitFor()) {
Bukkit.getScheduler().runTaskAsynchronously(DenizenDiscordBot.instance, () -> {
runner.run();
scriptEntry.setFinished(true);
});
} else {
Debug.echoError("DiscordMessage command ran without ~waitable. This will freeze the server. If you wanted your server to freeze, ignore this message.");
runner.run();
}
}
use of com.denizenscript.ddiscordbot.DiscordConnection in project dDiscordBot by DenizenScript.
the class DiscordMessageDeletedScriptEvent method getOldMessage.
public Message getOldMessage() {
if (oldMessage != null) {
return oldMessage;
}
DiscordConnection connection = getConnection();
if (connection == null) {
return null;
}
oldMessage = connection.cache.getMessage(getEvent().getChannel().getIdLong(), getEvent().getMessageIdLong());
return oldMessage;
}
use of com.denizenscript.ddiscordbot.DiscordConnection in project dDiscordBot by DenizenScript.
the class DiscordBotTag method registerTags.
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
// <--[tag]
// @attribute <DiscordBotTag.name>
// @returns ElementTag
// @plugin dDiscordBot
// @description
// Returns the name of the bot.
// -->
tagProcessor.registerTag(ElementTag.class, "name", (attribute, object) -> {
return new ElementTag(object.bot);
});
// <--[tag]
// @attribute <DiscordBotTag.self_user>
// @returns DiscordUserTag
// @plugin dDiscordBot
// @description
// Returns the bot's own Discord user object.
// -->
tagProcessor.registerTag(DiscordUserTag.class, "self_user", (attribute, object) -> {
DiscordConnection connection = object.getConnection();
if (connection == null) {
return null;
}
return new DiscordUserTag(object.bot, connection.client.getSelfUser());
});
// <--[tag]
// @attribute <DiscordBotTag.groups>
// @returns ListTag(DiscordGroupTag)
// @plugin dDiscordBot
// @description
// Returns a list of all groups (aka 'guilds' or 'servers') that this Discord bot has access to.
// -->
tagProcessor.registerTag(ListTag.class, "groups", (attribute, object) -> {
DiscordConnection connection = object.getConnection();
if (connection == null) {
return null;
}
ListTag list = new ListTag();
for (Guild guild : connection.client.getGuilds()) {
list.addObject(new DiscordGroupTag(object.bot, guild));
}
return list;
});
// <--[tag]
// @attribute <DiscordBotTag.commands>
// @returns ListTag(DiscordCommandTag)
// @plugin dDiscordBot
// @description
// Returns a list of all application commands.
// -->
tagProcessor.registerTag(ListTag.class, "commands", (attribute, object) -> {
DiscordConnection connection = object.getConnection();
if (connection == null) {
return null;
}
ListTag list = new ListTag();
for (Command command : connection.client.retrieveCommands().complete()) {
list.addObject(new DiscordCommandTag(object.bot, null, command));
}
return list;
});
// <--[tag]
// @attribute <DiscordBotTag.group[<name>]>
// @returns DiscordGroupTag
// @plugin dDiscordBot
// @description
// Returns the Discord group (aka 'guild' or 'server') that best matches the input name, or null if there's no match.
// -->
tagProcessor.registerTag(DiscordGroupTag.class, "group", (attribute, object) -> {
if (!attribute.hasParam()) {
return null;
}
DiscordConnection connection = object.getConnection();
if (connection == null) {
return null;
}
String matchString = CoreUtilities.toLowerCase(attribute.getParam());
Guild bestMatch = null;
for (Guild guild : connection.client.getGuilds()) {
String guildName = CoreUtilities.toLowerCase(guild.getName());
if (matchString.equals(guildName)) {
bestMatch = guild;
break;
}
if (guildName.contains(matchString)) {
bestMatch = guild;
}
}
if (bestMatch == null) {
return null;
}
return new DiscordGroupTag(object.bot, bestMatch);
});
// <--[tag]
// @attribute <DiscordBotTag.command[<name>]>
// @returns DiscordCommandTag
// @plugin dDiscordBot
// @description
// Returns the application command that best matches the input name, or null if there's no match.
// -->
tagProcessor.registerTag(DiscordCommandTag.class, "command", (attribute, object) -> {
if (!attribute.hasParam()) {
return null;
}
DiscordConnection connection = object.getConnection();
if (connection == null) {
return null;
}
String matchString = CoreUtilities.toLowerCase(attribute.getParam());
Command bestMatch = null;
for (Command command : connection.client.retrieveCommands().complete()) {
String commandName = CoreUtilities.toLowerCase(command.getName());
if (matchString.equals(commandName)) {
bestMatch = command;
break;
}
if (commandName.contains(matchString)) {
bestMatch = command;
}
}
if (bestMatch == null) {
return null;
}
return new DiscordCommandTag(object.bot, null, bestMatch);
});
}
use of com.denizenscript.ddiscordbot.DiscordConnection in project dDiscordBot by DenizenScript.
the class DiscordUserTag method getUser.
public User getUser() {
if (user != null) {
return user;
}
if (bot == null) {
return null;
}
DiscordConnection botObject = getBot();
if (botObject.client == null) {
return null;
}
user = botObject.client.getUserById(user_id);
return user;
}
Aggregations