Jump to content

[Release][Discord.js] Simple bot to grab server banners.


Xeon

Recommended Posts

  • Admin
Xeon

x360Labs Mod

Apprentice

  • Group:  Admin
  • Content Count:  9
  • Reputation:   14
  • Solved Content:  0
  • Days Won:  4

Hello, today I'll be releasing one of the bots I've made which can grab the banner image (and link) of any discord server it's in. There is two versions, a slash command version and a text command version. I have not combined them, this was a very fast project for me so the code may not be 10/10 but it runs in either configuration.

Requirements

- Node.js & Discord.js 

- A PC/Server to run the bot on

- The ability to create discord applications in the developer portal, set intents, and configure bot and server permissions.

Step 1: Install discord.js to your project directory, then create a file called "index.js"


Step 2: Copy & paste the code below (either the slash command version OR the text command version) into the index.js file with your text editor of choice, then save it.

 

Step 3: Run the bot through your console. On Windows this would be in a command prompt with the commands
"cd <your project directory here>" followed by "node index.js"


Slash command version:
 

const { Client, GatewayIntentBits, REST, Routes } = require('discord.js');
const token = 'your bot token goes here';  // Replace with your actual token
const clientId = 'your bots client id goes here';  // Replace with your bot's client ID

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

// Commands definition
const commands = [
  {
    name: 'banner',
    description: 'Displays the server banner'
  }
];

// Register commands globally
const rest = new REST({ version: '10' }).setToken(token);
(async () => {
  try {
    console.log('Started refreshing application (/) commands globally.');
    await rest.put(
      Routes.applicationCommands(clientId),
      { body: commands }
    );
    console.log('Successfully reloaded application (/) commands globally.');
  } catch (error) {
    console.error(error);
  }
})();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  const { commandName } = interaction;

  if (commandName === 'banner') {
    if (!interaction.guild) {
      await interaction.reply("This command can only be used in a server.");
      return;
    }

    if (interaction.guild.bannerURL()) {
      const bannerUrl = interaction.guild.bannerURL({ format: 'png', size: 1024 });
      await interaction.reply(`Server banner: ${bannerUrl}`);
    } else {
      await interaction.reply('This server does not have a banner set.');
    }
  }
});

client.login(token);

Text command version:
 

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

const token = 'your bot token goes here'; // Replace with your actual token

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('messageCreate', message => {
  // Check if the message is a command and not from a bot
  if (!message.content.startsWith('!') || message.author.bot) return;

  // Extract the command
  const command = message.content.slice(1).trim().split(' ')[0];

  if (command === 'banner') {
    console.log(`Command received: ${message.content}`); // Log only the command message

    if (!message.guild) {
      message.channel.send("This command can only be used in a server.");
      return;
    }

    // Fetching the banner URL if available
    if (message.guild.banner) {
      const bannerUrl = message.guild.bannerURL({ format: 'png', size: 1024 });
      message.channel.send(`Server banner: ${bannerUrl}`);
    } else {
      message.channel.send('This server does not have a banner set.');
    }
  }
});

client.login(token);

Screenshots:
spacer.pngspacer.png

spacer.png

Link to comment
Share on other sites

  • Support
Zac

x360Labs Mod

Contributor

  • Group:  Support
  • Content Count:  35
  • Reputation:   32
  • Solved Content:  0
  • Days Won:  7

i remember seeing you post something about this in the discord chat. Another thing to add to my resources folder for figuring out all this discord bot stuff.(never messed with it before)

Link to comment
Share on other sites

  • Admin
Xeon

x360Labs Mod

Apprentice

  • Group:  Admin
  • Content Count:  9
  • Reputation:   14
  • Solved Content:  0
  • Days Won:  4

4 hours ago, Zac said:

i remember seeing you post something about this in the discord chat. Another thing to add to my resources folder for figuring out all this discord bot stuff.(never messed with it before)

It could also easily be added as a command to an existing bot, I’ve just made it like this so it can be deployed and used by anyone who might come across this post. Also why I decided to embed the code instead of attaching files. I’ve actually got another thing to post which will be in this category at some point 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By viewing our site, you agree to our Terms of Use. Please abide by our rules.