├── ressources └── devicon.png ├── src ├── test │ └── java │ │ └── fr │ │ └── umontpellier │ │ └── iut │ │ └── AppTest.java └── main │ └── java │ └── fr │ └── umontpellier │ └── iut │ ├── commandes │ ├── Commande.java │ ├── Help.java │ └── RepertoireCommandes.java │ ├── App.java │ └── GestionnaireEvents.java ├── .github └── workflows │ └── maven.yml ├── README.md ├── .gitignore └── pom.xml /ressources/devicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZOKOU-M/Zoukou/HEAD/ressources/devicon.png -------------------------------------------------------------------------------- /src/test/java/fr/umontpellier/iut/AppTest.java: -------------------------------------------------------------------------------- 1 | package fr.umontpellier.iut; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/fr/umontpellier/iut/commandes/Commande.java: -------------------------------------------------------------------------------- 1 | package fr.umontpellier.iut.commandes; 2 | 3 | import net.dv8tion.jda.api.entities.Message; 4 | 5 | /** 6 | * Commande 7 | */ 8 | 9 | /** 10 | * Commande est l'interface qui représente une commande du bot discord 11 | *

12 | * Pour voir un exemple de commande : {@link Help} 13 | *

14 | * 15 | * @version 1.0 16 | */ 17 | public interface Commande { 18 | 19 | public void executer(Message messageRecue); 20 | } -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 11 20 | uses: actions/setup-java@v1 21 | with: 22 | java-version: 11 23 | - name: Build with Maven 24 | run: mvn -B package --file pom.xml 25 | -------------------------------------------------------------------------------- /src/main/java/fr/umontpellier/iut/commandes/Help.java: -------------------------------------------------------------------------------- 1 | package fr.umontpellier.iut.commandes; 2 | 3 | import net.dv8tion.jda.api.entities.Message; 4 | 5 | /** 6 | * Commande Help 7 | * 8 | * @see Commande 9 | * 10 | * @version 1.0 11 | */ 12 | 13 | /** 14 | * Help est la classe représentant une commande "help" du bot Discord. 15 | * 16 | * @see Commande 17 | * 18 | * @version 1.0 19 | */ 20 | public class Help implements Commande { 21 | 22 | /** 23 | * Envoie un message dans le channel où le message a été reçu. 24 | * 25 | * @param messageRecue {@code Message} 26 | * 27 | * @see Message#getChannel() 28 | * @see net.dv8tion.jda.api.entities.MessageChannel#sendMessage(CharSequence) 29 | */ 30 | @Override 31 | public void executer(Message messageRecue) { 32 | String reponse = " La demande d'aide a été entendue."; 33 | messageRecue.getChannel().sendMessage(reponse).queue(); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/fr/umontpellier/iut/App.java: -------------------------------------------------------------------------------- 1 | package fr.umontpellier.iut; 2 | 3 | import java.util.Scanner; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import net.dv8tion.jda.api.AccountType; 9 | import net.dv8tion.jda.api.JDABuilder; 10 | import net.dv8tion.jda.api.utils.Compression; 11 | 12 | /** 13 | * App est la classe qui permet d'exécuter le bot. 14 | * 15 | * @version 1.0 16 | */ 17 | public class App { 18 | 19 | private static final Logger LOG = LoggerFactory.getLogger(App.class); 20 | 21 | private static final JDABuilder DISCORD_BOT_BUILDER = new JDABuilder(AccountType.BOT) 22 | .addEventListeners(new GestionnaireEvents()).setCompression(Compression.NONE); 23 | 24 | public static void main(String[] args) { 25 | verifieArguments(args); 26 | JDABuilder builder = DISCORD_BOT_BUILDER.setToken(args[0]); 27 | login(builder); 28 | } 29 | 30 | private static void verifieArguments(String[] args) { 31 | if (args.length < 1) { 32 | throw new IllegalArgumentException("Veuillez mettre en argument le token du bot discord"); 33 | } 34 | } 35 | 36 | private static void login(JDABuilder builder) { 37 | try { 38 | for (int i = 0; i < 10; i++) 39 | builder.useSharding(i, 10).build(); 40 | } catch (javax.security.auth.login.LoginException e) { 41 | LOG.error("Un problème est survenu lors de la connexion.\n Veuillez récrire le token :", e); 42 | corrigerToken(builder); 43 | login(builder); 44 | } 45 | } 46 | 47 | private static void corrigerToken(JDABuilder builder) { 48 | Scanner in = new Scanner(System.in); 49 | builder.setToken(in.next()); 50 | in.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/fr/umontpellier/iut/GestionnaireEvents.java: -------------------------------------------------------------------------------- 1 | package fr.umontpellier.iut; 2 | 3 | import fr.umontpellier.iut.commandes.Commande; 4 | import fr.umontpellier.iut.commandes.RepertoireCommandes; 5 | import net.dv8tion.jda.api.events.message.MessageReceivedEvent; 6 | import net.dv8tion.jda.api.hooks.ListenerAdapter; 7 | 8 | /** 9 | * GestionnaireEvents est la classe qui gére tous les évenements du bot. 10 | * 11 | * @see ListenerAdapter 12 | * 13 | * @version 1.0 14 | */ 15 | public class GestionnaireEvents extends ListenerAdapter { 16 | 17 | /** 18 | * La désignation du bot. Cette variable représente le caractère qui permet 19 | * d'appeler le bot. 20 | * 21 | * Exemple : !help Pour déclancher la commande help du bot. 22 | * 23 | * @see GestionnaireEvents#botEstAppeleParUtilisateur(MessageReceivedEvent) 24 | */ 25 | private static final char DESIGNATION_BOT = '!'; 26 | 27 | /** 28 | * Exécute la commande choisie par l'utilisateur, s'il en a fait la demande. 29 | * 30 | * @param event {@code MessageReceivedEvent} Recéption du message d'un serveur 31 | * discord 32 | * 33 | * @see GestionnaireEvents#botEstAppeleParUtilisateur(MessageReceivedEvent) 34 | * @see Commande#executer(net.dv8tion.jda.api.entities.Message) 35 | * @see RepertoireCommandes#getCommande(MessageReceivedEvent) 36 | */ 37 | @Override 38 | public void onMessageReceived(MessageReceivedEvent event) { 39 | if (botEstAppeleParUtilisateur(event)) { 40 | Commande cmd = RepertoireCommandes.getCommande(event); 41 | if (cmd != null) 42 | cmd.executer(event.getMessage()); 43 | } 44 | } 45 | 46 | /** 47 | * Retourne vrai, si le premier caractère du message correspond à la désignation 48 | * du bot et s'il n'est l'utilisateur qui envoie le message n'est pas un bot. 49 | * 50 | * @param event {@code MessageReceivedEvent} message reçue par un serveur 51 | * discord 52 | * 53 | * @return {@code boolean} 54 | * 55 | * @see GestionnaireEvents#DESIGNATION_BOT 56 | * @see GestionnaireEvents#onMessageReceived(MessageReceivedEvent) 57 | */ 58 | private boolean botEstAppeleParUtilisateur(MessageReceivedEvent event) { 59 | return !event.getAuthor().isBot() && event.getMessage().getContentRaw().charAt(0) == DESIGNATION_BOT; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/fr/umontpellier/iut/commandes/RepertoireCommandes.java: -------------------------------------------------------------------------------- 1 | package fr.umontpellier.iut.commandes; 2 | 3 | import static java.util.Map.entry; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import net.dv8tion.jda.api.events.message.MessageReceivedEvent; 9 | 10 | /** 11 | * RepertoireCommandes est la classe qui répertorie toutes les commandes du 12 | * bot. 13 | *

14 | * Elle est caractérisée par : 15 | *

16 | * 20 | *

21 | * À noter que {@link #REPERTOIRE_DES_COMMANDES} est automatiquement initialisé 22 | * en static. 23 | *

24 | * 25 | * @see HashMap 26 | * @see Commande 27 | * 28 | * @version 1.0 29 | */ 30 | public class RepertoireCommandes { 31 | 32 | /** 33 | * Répertoire des commandes. Ce répertoire relie un mot-clé à une commande. 34 | * 35 | * @see HashMap 36 | * @see Commande 37 | */ 38 | private static final Map REPERTOIRE_DES_COMMANDES = Map.ofEntries(// 39 | entry("help", new Help())// Attention le string doit toujours être en minuscule 40 | ); 41 | 42 | /** 43 | * Retourne la commande associée au premier mot contenue dans le message 44 | * 45 | * @param msg {@code MessageReceivedEvent} message reçue par un serveur Discord 46 | * 47 | * @return {@code Commande} 48 | * 49 | * @see RepertoireCommandes#REPERTOIRE_DES_COMMANDES 50 | * @see java.util.HashMap#get(java.lang.Object) 51 | * @see RepertoireCommandes#getPremierMotDuMessage(MessageReceivedEvent) 52 | */ 53 | public static Commande getCommande(MessageReceivedEvent msg) { 54 | String motClef = getPremierMotDuMessage(msg); 55 | return REPERTOIRE_DES_COMMANDES.get(motClef); 56 | } 57 | 58 | /** 59 | * Retourne le premier mot contenu dans le message reçu du serveur 60 | * 61 | * @param msg {@code MessageReceivedEvent} Message reçu du serveur 62 | * 63 | * @return {@code String} Premier mot du message 64 | * 65 | * @see MessageReceivedEvent#getMessage() 66 | * @see net.dv8tion.jda.api.entities.Message#getContentRaw() 67 | */ 68 | private static String getPremierMotDuMessage(MessageReceivedEvent msg) { 69 | return msg.getMessage().getContentRaw().substring(1).split(" ")[0].toLowerCase(); 70 | } 71 | 72 | private RepertoireCommandes() { 73 | throw new IllegalStateException("Class utilitaire"); 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![GitHub Actions](https://github.com/DevLab-umontp/template-DiscordBot/workflows/Java%20CI%20with%20Maven/badge.svg) 2 | [![JavaDoc](https://img.shields.io/badge/JavaDoc-Online-green)](https://mathieusoysal.github.io/stats/template-discordbot) 3 | # ![](ressources/devicon.png) Template Discord Bot 4 | Créer votre propre bot Discord en quelques clics en récupérant le template ! 5 | 6 | [![](https://dabuttonfactory.com/button.png?t=Utiliser+le+template&f=Open+Sans-Bold&ts=14&tc=444&hp=20&vp=10&c=11&bgt=unicolored&bgc=f3f3f3&bs=2&bc=999&shs=1&shc=eee&sho=sw)](http://use.template-discordbot.umontp.fr/) 7 | ## Prérequis : 8 | 9 | - [Java SE 11 ou plus](https://docs.oracle.com/en/java/javase/11/install/overview-jdk-installation.html#GUID-8677A77F-231A-40F7-98B9-1FD0B48C346A) 10 | - [Maven](https://maven.apache.org/) 11 | - [Avoir obtenu le token de son bot](https://discord.com/developers/applications) 12 | 13 | ## Commande à effectuer pour lancer le bot : 14 | 15 | - `mvn clean compile assembly:single` 16 | - `java -jar fichier.jar TOKEN_DU_BOT` 17 | 18 | ## Rapide tuto pour ajouter une commande au sein du bot 19 | Le but du tuto est d'ajouter une commande *ping* au sein du bot, qui devra renvoyer le message *"pong!"*. 20 | 21 | ### Première étape créer la class `Ping` 22 | 23 | Rendez-vous dans le dossier qui se nomme *commandes* et créez-y le fichier `Ping.java` 24 | 25 | Au sein du fichier placez-y le code suivant : 26 | 27 | ```java 28 | package fr.umontpellier.iut.commandes; 29 | 30 | import net.dv8tion.jda.api.entities.Message; 31 | 32 | public class Ping implements Commande { 33 | 34 | @Override 35 | public void executer(Message messageRecue) { 36 | messageRecue.getChannel().sendMessage("pong!").queue(); 37 | } 38 | 39 | } 40 | ``` 41 | 42 | > Il est primordial que la class implémente l'interface *Commande* 43 | 44 | ### Deuxième étape associer un texte à la commande 45 | 46 | Dans notre cas nous voulons que notre bot lance la commande lorsqu'il reçois le message "ping". 47 | 48 | Pour cela rendez-vous dans le fichier `RepertoireCommandes.java` et ajoutez à la *ligne 39* le code suivant : 49 | ```java 50 | entry("ping", new Ping()), 51 | ``` 52 | 53 | ### Finiis 54 | Et voilà le résultat, une fois le bot [lancé](https://github.com/DevLab-umontp/template-DiscordBot/#commande-%C3%A0-effectuer-pour-lancer-le-bot-) : 55 | 56 | ![image](https://user-images.githubusercontent.com/43273304/107413672-4c151c00-6b11-11eb-98f4-e8e4ada5452a.png) 57 | 58 | À vous de laisser libre cours à votre imagination pour ajouter une multitude de nouvelles fonctionnalités. 59 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | # Compiled class file 26 | *.class 27 | 28 | # Log file 29 | *.log 30 | 31 | # BlueJ files 32 | *.ctxt 33 | 34 | # Mobile Tools for Java (J2ME) 35 | .mtj.tmp/ 36 | 37 | # Package Files # 38 | *.jar 39 | *.war 40 | *.nar 41 | *.ear 42 | *.zip 43 | *.tar.gz 44 | *.rar 45 | 46 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 47 | hs_err_pid* 48 | 49 | target/ 50 | pom.xml.tag 51 | pom.xml.releaseBackup 52 | pom.xml.versionsBackup 53 | pom.xml.next 54 | release.properties 55 | dependency-reduced-pom.xml 56 | buildNumber.properties 57 | .mvn/timing.properties 58 | .idea/ 59 | *.iml 60 | # CMake 61 | cmake-build-debug/ 62 | 63 | # Mongo Explorer plugin: 64 | .idea/**/mongoSettings.xml 65 | 66 | ## File-based project format: 67 | *.iws 68 | 69 | ## Plugin-specific files: 70 | 71 | # Eclipse 72 | .project 73 | .classpath 74 | .settings/ 75 | bin/ 76 | 77 | # IntelliJ 78 | /out/ 79 | .idea 80 | *.ipr 81 | *.iml 82 | *.iws 83 | 84 | # NetBeans 85 | nb-configuration.xml 86 | 87 | # Visual Studio Code 88 | .vscode 89 | .factorypath 90 | 91 | # OSX 92 | .DS_Store 93 | 94 | # Vim 95 | *.swp 96 | *.swo 97 | 98 | # mpeltonen/sbt-idea plugin 99 | .idea_modules/ 100 | 101 | # JIRA plugin 102 | atlassian-ide-plugin.xml 103 | 104 | # Cursive Clojure plugin 105 | .idea/replstate.xml 106 | 107 | # Crashlytics plugin (for Android Studio and IntelliJ) 108 | com_crashlytics_export_strings.xml 109 | crashlytics.properties 110 | crashlytics-build.properties 111 | fabric.properties 112 | 113 | .metadata 114 | bin/ 115 | tmp/ 116 | *.tmp 117 | *.bak 118 | *.swp 119 | *~.nib 120 | local.properties 121 | .settings/ 122 | .loadpath 123 | .recommenders 124 | 125 | # External tool builders 126 | .externalToolBuilders/ 127 | 128 | # Locally stored "Eclipse launch configurations" 129 | *.launch 130 | 131 | # PyDev specific (Python IDE for Eclipse) 132 | *.pydevproject 133 | 134 | # CDT-specific (C/C++ Development Tooling) 135 | .cproject 136 | 137 | # Java annotation processor (APT) 138 | .factorypath 139 | 140 | # PDT-specific (PHP Development Tools) 141 | .buildpath 142 | 143 | # sbteclipse plugin 144 | .target 145 | 146 | # Tern plugin 147 | .tern-project 148 | 149 | # TeXlipse plugin 150 | .texlipse 151 | 152 | # STS (Spring Tool Suite) 153 | .springBeans 154 | 155 | # Code Recommenders 156 | .recommenders/ 157 | 158 | # Scala IDE specific (Scala & Java development for Eclipse) 159 | .cache-main 160 | .scala_dependencies 161 | .worksheet 162 | 163 | nbproject/private/ 164 | build/ 165 | 166 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | fr.umontpellier.iut 7 | 8 | 9 | nom-du-bot 10 | jar 11 | 1.0-SNAPSHOT 12 | 13 | 14 | nomDuBot 15 | 16 | http://www.example.com 17 | 18 | 19 | UTF-8 20 | 11 21 | 11 22 | 23 | 24 | 25 | 26 | integration-test 27 | 28 | 29 | 30 | org.codehaus.mojo 31 | build-helper-maven-plugin 32 | 3.0.0 33 | 34 | 35 | add-integration-test-sources 36 | generate-test-sources 37 | 38 | add-test-source 39 | 40 | 41 | 42 | src/integration-test/java 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-failsafe-plugin 51 | 3.0.0-M3 52 | 53 | 54 | failsafe-integration-tests 55 | integration-test 56 | 57 | integration-test 58 | verify 59 | 60 | 61 | false 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | org.apache.logging.log4j 75 | log4j 76 | 2.14.0 77 | pom 78 | 79 | 80 | 81 | 82 | net.dv8tion 83 | JDA 84 | 4.1.1_125 85 | 86 | 87 | org.junit.jupiter 88 | junit-jupiter 89 | 5.5.1 90 | test 91 | 92 | 93 | org.junit.jupiter 94 | junit-jupiter-engine 95 | 5.4.0-M1 96 | test 97 | 98 | 99 | org.junit.vintage 100 | junit-vintage-engine 101 | 5.4.0-M1 102 | test 103 | 104 | 105 | org.junit.platform 106 | junit-platform-launcher 107 | 1.4.2 108 | test 109 | 110 | 111 | 112 | 113 | 114 | jcenter 115 | jcenter-bintray 116 | https://jcenter.bintray.com 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | org.apache.maven.plugins 126 | maven-surefire-plugin 127 | 2.22.2 128 | 129 | 130 | org.apache.maven.plugins 131 | maven-failsafe-plugin 132 | 2.22.0 133 | 134 | 135 | 136 | maven-clean-plugin 137 | 3.1.0 138 | 139 | 140 | 141 | maven-resources-plugin 142 | 3.0.2 143 | 144 | 145 | maven-compiler-plugin 146 | 3.8.0 147 | 148 | 11 149 | 11 150 | 151 | 152 | 153 | maven-assembly-plugin 154 | 3.0.0 155 | 156 | 157 | 158 | true 159 | fr.umontpellier.iut.App 160 | 161 | 162 | 163 | jar-with-dependencies 164 | 165 | 166 | 167 | 168 | maven-install-plugin 169 | 2.5.2 170 | 171 | 172 | maven-deploy-plugin 173 | 2.8.2 174 | 175 | 176 | 177 | maven-site-plugin 178 | 3.7.1 179 | 180 | 181 | maven-project-info-reports-plugin 182 | 3.0.0 183 | 184 | 185 | org.apache.maven.plugins 186 | maven-javadoc-plugin 187 | 3.0.0 188 | 189 | 11 190 | 11 191 | 192 | 193 | 194 | 195 | 196 | 197 | --------------------------------------------------------------------------------