Kann jemand mir den Code verbessern?

Es kommt der Fehler

“Bot token is missing. Please provide a valid toke.”

obwohl ich den Token in der .env-Datei gespeichert habe.

const { Client, GatewayIntentBits } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

require('dotenv').config();
const token = process.env.TOKEN;

if (!token) {
  console.error('Bot token is missing. Please provide a valid token.');
  process.exit(1);
}

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
  ],
});

// Load commands dynamically from a separate file
const commands = require('./commands');
const rest = new REST({ version: '9' }).setToken(token);
const GUILD_ID = '1101480796134588456';

// Function to register commands
const registerCommands = async () => {
  try {
    // Register commands for the specified guild
    await rest.put(
      Routes.applicationGuildCommands(client.user.id, GUILD_ID),
      { body: commands }
    );
    console.info('Successfully reloaded application (/) commands');
  }
  catch (error) {
    console.error('Error refreshing application (/) commands:', error);
  }
};

// Event handler for when the bot is ready
client.once('ready', () => {
  console.info(`Logged in as ${client.user.tag}!`);
  
  // Register commands after the bot is ready
  registerCommands();
});

// Anti-Raid
const raidCache = new Set();
const RAID_THRESHOLD = 7; // Number of members who join within a certain period of time to be considered a raid
const RAID_TIMEFRAME = 53000; // Time span in milliseconds

// Min age for Discord accounts
const MIN_ACCOUNT_AGE = 20; // in days

// Event handler for when a member joins the guild
client.on('guildMemberAdd', (member) => {
  raidCache.add(member.id);
  setTimeout(() => {
    raidCache.delete(member.id);
  }, RAID_TIMEFRAME);
  // Check the minimum age of the Discord account
  const accountAgeInDays = (Date.now() - member.user.createdTimestamp) / (1000 * 60 * 60 * 24);

  if (accountAgeInDays < MIN_ACCOUNT_AGE) {
    // Take action, e.g., kick a member, send notifications, etc.
    member.kick('Minimum age for Discord account not met.');
    console.info(`Member ${member.user.tag} was kicked for not meeting the minimum age requirement.`);
  }

  if (raidCache.size >= RAID_THRESHOLD) {
    // Take further measures for raid protection here
    member.kick('Anti-Raid: Suspicious behavior');
    console.info(`Member ${member.user.tag} was kicked because of suspicious behavior.`);
  }
});

// Anti-nuke
client.on('messageCreate', (message) => {
  if (message.author.bot) return;

  // Implement further checks for nuke protection here
  const messageContent = message.content.toLowerCase();

  if (messageContent.includes('nuke') || messageContent.includes('hs') || messageContent.includes('discord.gg/') || messageContent.includes('.gg/') || messageContent.includes('@everyone')) {
    // Take further measures for nuke protection here
    message.delete();
    message.author.send('Your message has been deleted due to suspicious activity');
  }
});

// Event handler for when an interaction (slash command) is created
client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;

  const commandName = interaction.commandName;

  if (commandName === 'ping') {
    await interaction.reply('Pong!');
  }
  // Add more commands as needed
});

// Event handler for any errors
client.on('error', (error) => {
  console.error('An error occurred:', error);
});

// Login to Discord with the provided token
client.login(token);
(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
5 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
guteantwort626
1 year ago

Well, you just have to save the tokens in the .env file in the Var TOKEN, then you can. It’s like…

guteantwort626
1 year ago
Reply to  egoisticdoor

and “npm i” have you already done?

Justman
1 year ago

The .env in the same directory as the bot?