Skip to content

Trying to control my haxball room with my discord bot #2078

Description

@Be7Nice

I have this code to send message from the haxball room to my discord server with my discord bot:
`const { Client, Events, GatewayIntentBits } = require("discord.js");

// Require the HaxballJS Promise that resolves HBInit function.
const HaxballJS = require("haxball.js");

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, (c) => {
console.log(Ready! Logged in as ${c.user.tag});
});

// When interaction (slash command, button, modal, select menu) created by user, run this code
client.on(Events.InteractionCreate, (interaction) => {
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === "announcement") {
const message = interaction.options.getString("message");

// Access the <RoomObject> with client.room
client.room.sendAnnouncement(`Discord Announcement: ${message}`);

}
});

// Log in to Discord with your client's token
client.login("My discord token");

// Pass the resolved HBInit function to CreateRoom function
HaxballJS.then(CreateRoom);

function CreateRoom(HBInit) {
// Same as in Haxball Headless Host Documentation https://github.com/haxball/haxball-issues/wiki/Headless-Host#api
const room = HBInit({
roomName: "Bambi ♥ Only Peace And Love",
maxPlayers: 16,
public: true,
noPlayer: true,
token: "haxball token", // Required https://haxball.com/headlesstoken
});

// Set client.room an for accessing it via
client.room = room;

room.setDefaultStadium("Big");
room.setScoreLimit(5);
room.setTimeLimit(0);

const map = new Map();
const dj = [];
const id = new Map();

function updateAdmins() {
var players = room.getPlayerList().filter((player) => player.id != 0);
if (players.length == 0) return;
if (players.find((player) => player.admin) != null) return;
room.setPlayerAdmin(players[0].id, true);
}

room.onGameTick = function() {
}

room.onPlayerJoin = function(player) {
updateAdmins()
map.set(player.id, player.auth);
id.set(player.auth, player.id);
}

room.onPlayerLeave = function(player) {
updateAdmins()
}

room.onPlayerChat = function(player, message) {

const auth = map.get(player.id);
if(message.substr(0, 3)==="!7") {
  if(dj.includes(player.id)) {
    room.sendAnnouncement(`You are already a DJ!`);
    return;
  }
  dj.push(auth);
  room.sendAnnouncement(`You are now a DJ!`);
}

if(dj.includes(auth)) {
if(message.substr(0, 1) == "!") {
  let song = message.substr(1);

client.channels.cache.get("My Channel ID").send(song);
}
}
}

room.onGameStart = function(player) {
}

room.onRoomLink = function (link) {
console.log(link);
};
}`
But I want to send message from discord to haxball, how can i do it only with my code and not with packages?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions