use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class ScheduleManager method createSchedule.
/**
* Create a new schedule and it's associated schedule channel, if the bot cannot create the
* new channel no schedule will be created
* @param gId (String) guild ID
* @param optional (String) optional name of schedule channel
*/
public void createSchedule(String gId, String optional) {
JDA jda = Main.getShardManager().getJDA(gId);
// bot self permissions
Collection<Permission> channelPerms = Stream.of(Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS, Permission.MESSAGE_MANAGE, Permission.MESSAGE_HISTORY, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.MESSAGE_ATTACH_FILES).collect(Collectors.toList());
// create channel and get ID
String cId;
try {
Guild guild = jda.getGuildById(gId);
cId = guild.createTextChannel(optional != null ? optional : "new_schedule").addPermissionOverride(// allow self permissions
guild.getMember(jda.getSelfUser()), channelPerms, new ArrayList<>()).addPermissionOverride(// disable @everyone message write
guild.getPublicRole(), // disable @everyone message write
new ArrayList<>(), Collections.singletonList(Permission.MESSAGE_WRITE)).complete().getId();
} catch (PermissionException e) {
String m = e.getMessage() + ": Guild ID " + gId;
Logging.warn(this.getClass(), m);
return;
} catch (Exception e) {
Logging.exception(this.getClass(), e);
return;
}
// create the schedule database entry
createNewSchedule(cId, gId);
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class ScheduleManager method createSchedule.
/**
* Convert a pre-existing discord channel to a new saber schedule channel
* @param channel (TextChannel) a pre-existing channel to convert to a schedule
*/
public void createSchedule(TextChannel channel) {
JDA jda = Main.getShardManager().getJDA(channel.getGuild().getId());
// bot self permissions
Collection<Permission> channelPerms = Stream.of(Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS, Permission.MESSAGE_MANAGE, Permission.MESSAGE_HISTORY, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.MESSAGE_ATTACH_FILES).collect(Collectors.toList());
// attempt to set the channel permissions
try {
// self perms
channel.createPermissionOverride(channel.getGuild().getMember(jda.getSelfUser())).setAllow(channelPerms).queue();
// @everyone perms
channel.createPermissionOverride(channel.getGuild().getPublicRole()).setDeny(Collections.singleton(Permission.MESSAGE_WRITE)).queue();
} catch (PermissionException e) {
String m = e.getMessage() + ": Guild ID " + channel.getGuild().getId();
Logging.warn(this.getClass(), m);
} catch (Exception e) {
Logging.exception(this.getClass(), e);
}
// create the schedule database entry
createNewSchedule(channel.getId(), channel.getGuild().getId());
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class ScheduleEntry method getMessageObject.
/**
* Attempts to retrieve the discord Message, if the message does not exist
* (or the bot can for any other reason cannot retrieve it) the method returns null
* @return (Message) if exists, otherwise null
*/
public Message getMessageObject() {
Message msg;
try {
JDA jda = Main.getShardManager().isSharding() ? Main.getShardManager().getShard(guildId) : Main.getShardManager().getJDA();
msg = jda.getTextChannelById(this.chanId).retrieveMessageById(this.msgId).complete();
} catch (Exception e) {
msg = null;
}
return msg;
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class ScheduleEntry method spawnRole.
/**
* generates a temporary RSVP group role for dynamic user mentioning
* the role will last for some time before being removed
* @param group the rsvp group
* @return the newly created Role
*/
public Role spawnRole(String group) {
List<String> members = this.rsvpMembers.get(group);
JDA jda = Main.getShardManager().getJDA(this.guildId);
Guild guild = jda.getGuildById(guildId);
// create the event RSVP role
Role role = guild.createRole().setName(group).setMentionable(true).setColor(Color.ORANGE).complete();
// add all RSVP'ed members to the role
members.forEach(memberId -> {
if (memberId.matches("\\d+")) {
Member member = guild.getMemberById(memberId);
guild.addRoleToMember(member, role).reason("dynamic RSVP role for event announcement").complete();
}
});
// automatically remove the role after 5 minutes
role.delete().queueAfter(60 * 5, TimeUnit.SECONDS);
return role;
}
use of net.dv8tion.jda.api.JDA in project Saber-Bot by notem.
the class ScheduleEntry method announcementsToString.
/**
* creates string display for event announcements
*/
public String announcementsToString() {
StringBuilder body = new StringBuilder("// Event Announcements\n");
JDA jda = Main.getShardManager().getJDA(this.guildId);
for (String id : this.aTimes.keySet()) {
TextChannel channel = jda.getTextChannelById(this.aTargets.get(id));
body.append("(").append(Integer.parseInt(id) + 1).append(") \"").append(this.aMessages.get(id)).append("\"").append(" at \"").append(this.aTimes.get(id)).append("\"").append(" to \"#").append(channel == null ? "unknown_channel" : channel.getName()).append("\"\n");
}
return body.toString();
}
Aggregations