Last active
June 17, 2022 21:15
-
-
Save jagrosh/8f9800563ba22ace2aa65bb3342dfb41 to your computer and use it in GitHub Desktop.
Discord Bot to sync statuses to roles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
token= | |
guild_id= | |
streaming_id= | |
online_id= | |
idle_id= | |
dnd_id= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 John Grosh ([email protected]). | |
* | |
* 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. | |
*/ | |
package com.jagrosh.statusrolebot; | |
import java.io.FileReader; | |
import java.util.Collections; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Properties; | |
import net.dv8tion.jda.core.AccountType; | |
import net.dv8tion.jda.core.JDABuilder; | |
import net.dv8tion.jda.core.entities.Game; | |
import net.dv8tion.jda.core.entities.Guild; | |
import net.dv8tion.jda.core.entities.Member; | |
import net.dv8tion.jda.core.entities.Role; | |
import net.dv8tion.jda.core.events.Event; | |
import net.dv8tion.jda.core.events.ReadyEvent; | |
import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent; | |
import net.dv8tion.jda.core.events.user.UserGameUpdateEvent; | |
import net.dv8tion.jda.core.events.user.UserOnlineStatusUpdateEvent; | |
import net.dv8tion.jda.core.hooks.EventListener; | |
/** | |
* | |
* @author John Grosh ([email protected]) | |
*/ | |
public class StatusRoleBot implements EventListener | |
{ | |
private final long guild; | |
private final long[] statuses; | |
private StatusRoleBot(long guild, long streaming, long online, long idle, long dnd) | |
{ | |
this.guild = guild; | |
this.statuses = new long[4]; | |
this.statuses[0] = streaming; | |
this.statuses[1] = online; | |
this.statuses[2] = idle; | |
this.statuses[3] = dnd; | |
} | |
private long memberToStatus(Member m) | |
{ | |
if(m.getGame()!=null && m.getGame().getType()==Game.GameType.STREAMING) | |
return statuses[0]; | |
switch(m.getOnlineStatus()) | |
{ | |
case ONLINE: return statuses[1]; | |
case IDLE: return statuses[2]; | |
case DO_NOT_DISTURB: return statuses[3]; | |
default: return 0; | |
} | |
} | |
@Override | |
public void onEvent(Event event) | |
{ | |
if(event instanceof UserOnlineStatusUpdateEvent) | |
update(((UserOnlineStatusUpdateEvent)event).getMember()); | |
else if(event instanceof UserGameUpdateEvent) | |
update(((UserGameUpdateEvent)event).getMember()); | |
else if(event instanceof GuildMemberJoinEvent) | |
update(((GuildMemberJoinEvent)event).getMember()); | |
else if(event instanceof ReadyEvent) | |
{ | |
Guild g = event.getJDA().getGuildById(guild); | |
if(g!=null) | |
g.getMembers().forEach(m -> update(m)); | |
} | |
} | |
private void update(Member member) | |
{ | |
if(member.getGuild().getIdLong()!=guild) | |
return; | |
long should = memberToStatus(member); | |
Role role = member.getGuild().getRoleById(should); | |
boolean needsChange = role==null ? false : !member.getRoles().contains(role); | |
List<Role> remList = new LinkedList<>(); | |
for(long se: statuses) | |
{ | |
if(se==should) | |
continue; | |
Role sr = member.getGuild().getRoleById(se); | |
if(sr!=null && member.getRoles().contains(sr)) | |
{ | |
needsChange = true; | |
remList.add(sr); | |
} | |
} | |
if(needsChange) | |
member.getGuild().getController().modifyMemberRoles(member, role==null ? Collections.EMPTY_LIST : Collections.singleton(role), remList).queue(); | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
Properties config = new Properties(); | |
config.load(new FileReader("config.txt")); | |
new JDABuilder(AccountType.BOT) | |
.setToken(config.getProperty("token")) | |
.addEventListener(new StatusRoleBot(Long.parseLong(config.getProperty("guild_id")), | |
Long.parseLong(config.getProperty("streaming_id")),Long.parseLong(config.getProperty("online_id")), | |
Long.parseLong(config.getProperty("idle_id")),Long.parseLong(config.getProperty("dnd_id")))) | |
.buildAsync(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment