Skip to content

Instantly share code, notes, and snippets.

@jagrosh
Created February 2, 2018 19:47
Show Gist options
  • Save jagrosh/f358671db43ab81ad852963337a7dca3 to your computer and use it in GitHub Desktop.
Save jagrosh/f358671db43ab81ad852963337a7dca3 to your computer and use it in GitHub Desktop.
Bot to give (or take away) roles based on clicking certain reactions.
/*
* Copyright 2018 John Grosh (jagrosh).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.MessageReaction.ReactionEmote;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionRemoveEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author John Grosh ([email protected])
*/
public class RoleReactionBot extends ListenerAdapter
{
private final static String FILENAME = "role-reactions.txt";
private final static Logger LOG = LoggerFactory.getLogger(RoleReactionBot.class);
private final HashMap<String,Long> map;
public RoleReactionBot() throws Exception
{
map = new HashMap<>();
Files.readAllLines(Paths.get(FILENAME), StandardCharsets.UTF_8).forEach(line ->
{
String[] split = line.replace("\uFEFF", "").split(" ", 3);
try
{
long messageId = Long.parseLong(split[0].trim());
long roleId = Long.parseLong(split[2]);
map.put(key(messageId, split[1]), roleId);
}
catch(Exception e)
{
LOG.warn("Could not parse: "+line);
}
});
}
@Override
public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event)
{
Role role = event.getGuild().getRoleById(map.getOrDefault(key(event.getMessageIdLong(),event.getReactionEmote()), 0L));
if(role!=null)
event.getGuild().getController().addSingleRoleToMember(event.getMember(), role).queue();
}
@Override
public void onGuildMessageReactionRemove(GuildMessageReactionRemoveEvent event)
{
Role role = event.getGuild().getRoleById(map.getOrDefault(key(event.getMessageIdLong(),event.getReactionEmote()), 0L));
if(role!=null)
event.getGuild().getController().removeSingleRoleFromMember(event.getMember(), role).queue();
}
private static String key(long messageId, ReactionEmote emote)
{
return key(messageId, emote.getEmote()==null ? emote.getName(): emote.getEmote().getId());
}
private static String key(long messageId, String emoji)
{
return messageId+"-"+emoji;
}
public static void main(String[] args) throws Exception
{
new JDABuilder(AccountType.BOT)
.setToken(Files.readAllLines(Paths.get("config.txt")).get(0))
.addEventListener(new RoleReactionBot())
.buildAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment