Log warnings to a channel
This commit is contained in:
		
					parent
					
						
							
								7ee79862c8
							
						
					
				
			
			
				commit
				
					
						49c7984bde
					
				
			
		
					 6 changed files with 40 additions and 8 deletions
				
			
		|  | @ -21,7 +21,8 @@ A multipurpose Discord bot written in Java. | ||||||
| ## Config file schema | ## Config file schema | ||||||
| ```json | ```json | ||||||
| { | { | ||||||
|   "token": "your_token" |   "token": "your_token", | ||||||
|  |   "warningChannel": 0 | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
|  | @ -37,3 +38,4 @@ A multipurpose Discord bot written in Java. | ||||||
| | `/timeout get`    | Gets the current timeout status of the specified member                      | `[member]`                                                | | | `/timeout get`    | Gets the current timeout status of the specified member                      | `[member]`                                                | | ||||||
| | `/timeout cancel` | Cancels the specified users timeout                                          | `[member]`, `(reason)`                                    | | | `/timeout cancel` | Cancels the specified users timeout                                          | `[member]`, `(reason)`                                    | | ||||||
| | `/poll`           | Creates a poll                                                               | `[title]`, `[duration]`, `[options]`, `(multiple-choice)` |  | | `/poll`           | Creates a poll                                                               | `[title]`, `[duration]`, `[options]`, `(multiple-choice)` |  | ||||||
|  | | `/warn`           | Warns a user                                                                 | `[member]`, `[reason]`                                    | | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
| { | { | ||||||
|   "token": "CHANGEME" |   "token": "CHANGEME", | ||||||
|  |   "warningChannel": 0 | ||||||
| } | } | ||||||
							
								
								
									
										2
									
								
								pom.xml
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
										
									
									
									
								
							|  | @ -6,7 +6,7 @@ | ||||||
| 
 | 
 | ||||||
|     <groupId>net.hypr</groupId> |     <groupId>net.hypr</groupId> | ||||||
|     <artifactId>doki</artifactId> |     <artifactId>doki</artifactId> | ||||||
|     <version>1.0.6</version> |     <version>1.0.7</version> | ||||||
| 
 | 
 | ||||||
|     <properties> |     <properties> | ||||||
|         <maven.compiler.source>17</maven.compiler.source> |         <maven.compiler.source>17</maven.compiler.source> | ||||||
|  |  | ||||||
|  | @ -11,6 +11,7 @@ import java.nio.charset.StandardCharsets; | ||||||
| //You will need a valid config.json in the resources folder for this to work | //You will need a valid config.json in the resources folder for this to work | ||||||
| public class Config { | public class Config { | ||||||
|     @SuppressWarnings("unused") private String token; |     @SuppressWarnings("unused") private String token; | ||||||
|  |     @SuppressWarnings("unused") private long warningChannel; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Returns the configuration object for this bot |      * Returns the configuration object for this bot | ||||||
|  | @ -33,4 +34,7 @@ public class Config { | ||||||
|     public String getToken() { |     public String getToken() { | ||||||
|         return token; |         return token; | ||||||
|     } |     } | ||||||
|  |     public long getWarningChannel() { | ||||||
|  |         return warningChannel; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | @ -22,6 +22,10 @@ public class Doki { | ||||||
|     public static JDA getJDA() { |     public static JDA getJDA() { | ||||||
|         return jda; |         return jda; | ||||||
|     } |     } | ||||||
|  |     public static Config getConfig() { return config; } | ||||||
|  |     public static Logger getLogger() { | ||||||
|  |         return log; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     public static void start() throws IOException, InterruptedException { |     public static void start() throws IOException, InterruptedException { | ||||||
|         config = Config.readConfig(); |         config = Config.readConfig(); | ||||||
|  |  | ||||||
|  | @ -10,6 +10,11 @@ import com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand | ||||||
| import net.dv8tion.jda.api.EmbedBuilder; | import net.dv8tion.jda.api.EmbedBuilder; | ||||||
| import net.dv8tion.jda.api.Permission; | import net.dv8tion.jda.api.Permission; | ||||||
| import net.dv8tion.jda.api.entities.Member; | import net.dv8tion.jda.api.entities.Member; | ||||||
|  | import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||||||
|  | import net.hypr.doki.Doki; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | 
 | ||||||
|  | import java.awt.*; | ||||||
| 
 | 
 | ||||||
| @CommandMarker | @CommandMarker | ||||||
| @BotPermissions(Permission.MODERATE_MEMBERS) | @BotPermissions(Permission.MODERATE_MEMBERS) | ||||||
|  | @ -22,13 +27,29 @@ public class Warn extends ApplicationCommand { | ||||||
|     public void warn(GuildSlashEvent event, |     public void warn(GuildSlashEvent event, | ||||||
|                      @AppOption(name = "member", description = "The member to warn") Member member, |                      @AppOption(name = "member", description = "The member to warn") Member member, | ||||||
|                      @AppOption(name = "reason", description = "The reason for warning them") String reason) { |                      @AppOption(name = "reason", description = "The reason for warning them") String reason) { | ||||||
|  |         Logger log = Doki.getLogger(); | ||||||
|  |         Member actingMod = event.getMember(); | ||||||
|         EmbedBuilder warnEmbed = new EmbedBuilder() |         EmbedBuilder warnEmbed = new EmbedBuilder() | ||||||
|                 .setTitle(String.format("You were warned in %s", event.getGuild().getName())) |                 .setTitle(String.format("You were warned in %s", event.getGuild().getName())) | ||||||
|                 .setDescription(String.format("**Reason:** %s", reason)); |                 .setDescription(String.format("**Reason:** %s", reason)) | ||||||
|         member.getUser().openPrivateChannel().queue((dm) -> dm.sendMessageEmbeds(warnEmbed.build()).queue( |                 .setFooter(String.format("Warned by %s (%s)", actingMod.getEffectiveName(), actingMod.getId()), actingMod.getEffectiveAvatarUrl()) | ||||||
|  |                 .setColor(new Color(239, 148, 60)); | ||||||
|  |         log.debug(String.format("%s", Doki.getConfig().getWarningChannel())); | ||||||
|  |         TextChannel warningChannel = Doki.getJDA().getTextChannelById(Doki.getConfig().getWarningChannel()); | ||||||
|  |         member.getUser().openPrivateChannel().queue( | ||||||
|  |                 (dm) -> dm.sendMessageEmbeds(warnEmbed.build()).queue( | ||||||
|                     success -> event.replyFormat("Warned %s for %s", member.getAsMention(), reason).setEphemeral(true).queue(), |                     success -> event.replyFormat("Warned %s for %s", member.getAsMention(), reason).setEphemeral(true).queue(), | ||||||
|                     // User has blocked bot or disabled DMs |                     // User has blocked bot or disabled DMs | ||||||
|                     error -> event.replyFormat("Warned %s for %s\n-# (Unable to DM user)", member.getAsMention(), reason).queue() |                     error -> event.replyFormat("Warned %s for %s\n-# (Unable to DM user)", member.getAsMention(), reason).queue() | ||||||
|         )); |                 ), | ||||||
|  |                 error -> event.replyFormat("Warned %s for %s\n-# (Unable to DM user)", member.getAsMention(), reason).queue() | ||||||
|  |         ); | ||||||
|  |         warnEmbed.setTitle(String.format("%s (%s) was warned", member.getEffectiveName(), member.getId())); | ||||||
|  |         try { | ||||||
|  |             assert warningChannel != null; | ||||||
|  |             warningChannel.sendMessageEmbeds(warnEmbed.build()).queue(); | ||||||
|  |         } catch (NullPointerException ex) { | ||||||
|  |             log.warn("Failed to send message to warning log channel"); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 floppydiskette
				floppydiskette