build chat system
This commit is contained in:
20
web/server/api/minecraft/message/chattodiscord.js
Normal file
20
web/server/api/minecraft/message/chattodiscord.js
Normal file
@@ -0,0 +1,20 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { content, uuid } = await readBody(event);
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
const coll = db.collection('users')
|
||||
const doc = await coll.findOne({ 'minecraft.uuid': uuid })
|
||||
|
||||
await $fetch(config.discordHost + '/minecraft/sendchatmessage', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
username: doc.discord.username + ' | ' + doc.minecraft.username,
|
||||
// avatarURL: 'https://cdn.discordapp.com/avatars/' + doc.discord.id + '/' + doc.discord.avatarHash + '.png',
|
||||
avatarURL: 'https://api.mineatar.io/face/' + doc.minecraft.uuid + '?scale=16',
|
||||
content: content,
|
||||
}
|
||||
})
|
||||
|
||||
return { code: 'success' }
|
||||
});
|
10
web/server/api/minecraft/message/chattominecraft.js
Normal file
10
web/server/api/minecraft/message/chattominecraft.js
Normal file
@@ -0,0 +1,10 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { discordId, content } = await readBody(event);
|
||||
|
||||
const coll = db.collection('users');
|
||||
const doc = await coll.findOne({ 'discord.id': discordId });
|
||||
|
||||
await sendRconCommand(`tellraw @a {"text":"(DC) ${doc.discord.username} > ${content}"}`)
|
||||
|
||||
return { whoo: 'hi' }
|
||||
});
|
18
web/server/api/minecraft/message/gametodiscord.js
Normal file
18
web/server/api/minecraft/message/gametodiscord.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { content, uuid } = await readBody(event);
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
const coll = db.collection('users')
|
||||
const doc = await coll.findOne({ 'minecraft.uuid': uuid })
|
||||
|
||||
await $fetch(config.discordHost + '/minecraft/sendgamemessage', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
avatarURL: 'https://api.mineatar.io/face/' + doc.minecraft.uuid + '?scale=16',
|
||||
content: content,
|
||||
}
|
||||
})
|
||||
|
||||
return { code: 'success' }
|
||||
});
|
41
web/server/utils/rcon.js
Normal file
41
web/server/utils/rcon.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import { RCON } from 'minecraft-server-util';
|
||||
|
||||
const client = new RCON()
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
let connected = false;
|
||||
|
||||
export const sendRconCommand = async (command) => {
|
||||
await connectRcon()
|
||||
|
||||
const message = await client.execute(command);
|
||||
|
||||
closeClient()
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
const connectRcon = async () => {
|
||||
if (connected) return;
|
||||
|
||||
await client.connect(config.rconHost);
|
||||
await client.login(config.rconPassword);
|
||||
|
||||
connected = true;
|
||||
}
|
||||
|
||||
const debounce = (callback, wait) => {
|
||||
let timeout = null;
|
||||
return (...args) => {
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(() => {
|
||||
callback.apply(null, args);
|
||||
}, wait);
|
||||
};
|
||||
}
|
||||
|
||||
const closeClient = debounce(() => {
|
||||
client.close();
|
||||
connected = false;
|
||||
}, 5000)
|
Reference in New Issue
Block a user