Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

node.js - Discord bot not able to contact discord servers when using docker compose

I recently started learning docker and have been running into issues when using docker-compose The discord bot works perfectly when you use docker run but whenever I run docker-compose up, I get the following error

app_1  | /app/node_modules/discord.js/src/rest/RequestHandler.js:93
app_1  |         throw new HTTPError(error.message, error.constructor.name, error.status, request.method, request.path);
app_1  |               ^
app_1  | 
app_1  | HTTPError [FetchError]: request to https://discord.com/api/v7/gateway/bot failed, reason: getaddrinfo EAI_AGAIN discord.com
app_1  |     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:93:15)
app_1  |     at processTicksAndRejections (node:internal/process/task_queues:93:5)
app_1  |     at async RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:39:14)
app_1  |     at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:138:9)
app_1  |     at async Client.login (/app/node_modules/discord.js/src/client/Client.js:223:7) {
app_1  |   code: 500,
app_1  |   method: 'get',
app_1  |   path: '/gateway/bot'
app_1  | }
app_1  | npm ERR! code 1
app_1  | npm ERR! path /app
app_1  | npm ERR! command failed
app_1  | npm ERR! command sh -c tsc && node dist/index.js
app_1  | 
app_1  | npm ERR! A complete log of this run can be found in:
app_1  | npm ERR!     /root/.npm/_logs/2021-01-11T03_27_59_096Z-debug.log
bot_app_1 exited with code 1

Here is my dockerfile

FROM node

WORKDIR /app

COPY package*.json ./
COPY .env ./

RUN npm install

COPY . .

CMD ["npm","start"]

Here is my docker-compose.yml file

version: '3.9'

services:
  db:
    image: postgres:13
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD
  app:
    build: .
    volumes:
      - app-data:/app/data
    depends_on:
      - db
    env_file: .env
    environment:
      POSTGRES_HOST: db

volumes:
  app-data: {}
  db-data: {}

And here is my main code which helps me connect to discord

import { Client, Message, Guild } from "discord.js";
import { commands } from "./commands/index";
import { config } from "dotenv";
export const client = new Client();
config();
client.on("ready", async () => {
  const guild = await client.guilds.fetch("771187253937438762");
  console.log(`Logged in as ${client.user.tag}!`);
  client.user.setActivity(`over ${guild.memberCount} furries`, {
    type: "WATCHING",
  });
});
client.on("message", async (msg) => {
  if (msg.author.bot) return;
  if (msg.author.id == client.user?.id) return;
  if (!msg.content.startsWith(process.env.BOT_PREFIX)) return;
  const [cmd, ...args] = msg.content
    .slice(process.env.BOT_PREFIX.length)
    .replace(/ +/g, " ")
    .split(" ");
  const commandClass = commands.find((command) => {
    return command.aliases.some((c) => c.toLowerCase() === cmd.toLowerCase());
  });
  if (!commandClass) return;

    await commandClass.execute({
      msg: msg as Message & { guild: Guild },
      cmd,
      args: args,
    }).catch((e)=>{
      console.log(e);
    });
});
client.login(process.env.DISCORD_TOKEN);

If anyone can help me with this question that would be great.

question from:https://stackoverflow.com/questions/65660994/discord-bot-not-able-to-contact-discord-servers-when-using-docker-compose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing the network for your discord bot in your docker-compose.yml

network_mode: bridge - This is the easiest way to get it to work

You can incorperate this into the compose file like:

version: '3.9'

services:
  db:
    image: postgres:13
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD
  app:
    build: .
    volumes:
      - app-data:/app/data
    depends_on:
      - db
    env_file: .env
    environment:
      POSTGRES_HOST: db
    network_mode: bridge

volumes:
  app-data: {}
  db-data: {}

You can read more about the network types in the docs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...