Skip to content
Snippets Groups Projects
Commit aef5afc7 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

envoie des messages du tchat mc

parent 319cd6a6
No related branches found
No related tags found
No related merge requests found
...@@ -238,38 +238,16 @@ recursiveAsyncReadLine(); //we have to actually start our recursion somehow ...@@ -238,38 +238,16 @@ recursiveAsyncReadLine(); //we have to actually start our recursion somehow
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// API
////////////////////////////////////////////////////////////////
app.get('/ping', (req,res) => {
res.send("Pong!")
})
app.get('/channels/:id', (req,res) => {
var id = req.params.id;
res.send(client.channels.cache.get(id));
})
app.get('/mc/chat/:detail', (req,res) => {
var detail = JSON.parse(decodeURI(req.params.detail));
res.send(detail.test);
})
app.listen(27001, () => {
console.log('['+'INFO'.yellow+'] Écoute sur '.brightWhite+'node.sl-projects.com:27001'.yellow);
});
////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
// BOT DISCORD // BOT DISCORD
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
// require the needed discord.js classes // require the needed discord.js classes
const { Client, Intents, MessageActionRow, MessageButton } = require('discord.js'); const { Client, Intents, MessageActionRow, MessageButton } = require('discord.js');
const { verify } = require('crypto');
// create a new Discord client // create a new Discord client
const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES", "GUILD_MEMBERS"] }); const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES", "GUILD_MEMBERS"] });
var mcChatWebhook;
client.on("ready", async function () { client.on("ready", async function () {
console.log('\n' + "SL-Projects Bot".brightCyan); console.log('\n' + "SL-Projects Bot".brightCyan);
...@@ -282,6 +260,8 @@ client.on("ready", async function () { ...@@ -282,6 +260,8 @@ client.on("ready", async function () {
client.user.setActivity("Regarde "+memberCount+" membres"); client.user.setActivity("Regarde "+memberCount+" membres");
checkAnniv(); checkAnniv();
verifyMcChatWebhook();
}); });
// Actions lorsqu'un membre rejoins ou part // Actions lorsqu'un membre rejoins ou part
...@@ -473,7 +453,50 @@ async function checkAnniv() { ...@@ -473,7 +453,50 @@ async function checkAnniv() {
} }
} }
} }
}
function verifyMcChatWebhook(){
let mcChatWebhookId = botSettings.findOne({ where: { name: "mcChatWebhookId" } });
if(mcChatWebhookId){
console.log("['" + 'INFO'.yellow + "] Le Webhook du serveur MC est déjà configuré.");
let mcChatWebhookToken = botSettings.findOne({ where: { name: "mcChatWebhookToken" } });
if(mcChatWebhookToken){
mcChatWebhook = new Discord.WebhookClient(mcChatWebhookId.value, mcChatWebhookToken.value);
}else{
console.log("['" + 'ERREUR'.brightRed + "] Impossible de trouver le token du webhook dans la base de donnée.");
createMcChatWebhook();
}
}else{
createMcChatWebhook();
}
}
function createMcChatWebhook(){
console.log("['" + 'INFO'.yellow + "] Création du Webhook du serveur MC.");
client.channels.cache.get(config.get("CANAL_CHAT_MC")).createWebhook('Chat Serveur Minecraft', {
avatar: 'https://sl-craft.fr/data/images/logo/short-color.png',
})
.then(webhook => {
botSettings.create({
name: "mcChatWebhookId",
value: webhook.id
});
botSettings.create({
name: "mcChatWebhookToken",
value: webhook.token
});
mcChatWebhook = new Discord.WebhookClient(webhook.id, webhook.token);
console.log("['" + 'INFO'.yellow + "] Le Webhook du serveur MC a été configuré avec succès.");
})
.catch(console.error);
}
function sendMessageFromMcChat(username, message){
webhookClient.send({
content: message,
username: username,
avatarURL: 'https://live.mc.sl-projects.com/tiles/faces/32x32/' + username + '.png',
});
} }
...@@ -485,3 +508,27 @@ const job = schedule.scheduleJob('0 0 * * *', function(){ ...@@ -485,3 +508,27 @@ const job = schedule.scheduleJob('0 0 * * *', function(){
// login to Discord with your app's token // login to Discord with your app's token
client.login(config.get("DISCORD_BOT_TOKEN")); client.login(config.get("DISCORD_BOT_TOKEN"));
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
// API
////////////////////////////////////////////////////////////////
app.get('/ping', (req,res) => {
res.send("Pong!")
})
app.get('/channels/:id', (req,res) => {
var id = req.params.id;
res.send(client.channels.cache.get(id));
})
app.get('/mc/chat/:detail', (req,res) => {
var detail = JSON.parse(decodeURI(req.params.detail));
sendMessageFromMcChat(detail.username, detail.message);
})
app.listen(27001, () => {
console.log('['+'INFO'.yellow+'] Écoute sur '.brightWhite+'node.sl-projects.com:27001'.yellow);
});
////////////////////////////////////////////////////////////////
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment