From 55534ba5f11d33d69b923e855142554d799cd8cf Mon Sep 17 00:00:00 2001 From: lilyissillyyy Date: Sun, 7 Sep 2025 00:22:49 -0400 Subject: [PATCH] Remove BG Command --- .env.example | 1 + README.md | 6 +++- commands/edit-image/remove-bg.js | 54 ++++++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 commands/edit-image/remove-bg.js diff --git a/.env.example b/.env.example index 93852ca4..518deff5 100644 --- a/.env.example +++ b/.env.example @@ -37,6 +37,7 @@ GITHUB_ACCESS_TOKEN= GOOGLE_KEY= GOV_KEY= IDIOT_URL= +REMOVEBG_KEY= SAUCENAO_KEY= SPOTIFY_KEY= SPOTIFY_SECRET= diff --git a/README.md b/README.md index 772fdb0a..ab680db1 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Only if you want to use the DECTalk command. * `GOOGLE_KEY` is your API key for Google, used for the YouTube Data API. You can get one [here](https://console.cloud.google.com/apis/dashboard). You must also [activate the YouTube Data API](https://console.cloud.google.com/marketplace/product/google/youtube.googleapis.com?q=search). * `GOV_KEY` is your API key for NASA. You get get one [here](https://api.nasa.gov/). * `IDIOT_URL` is a URL, ideally to the [Wikipedia page of an idiot](https://en.wikipedia.org/wiki/Donald_Trump). By default, it will reply with "_Stares at you._" +* `REMOVEBG_KEY` is your API key for remove.bg. You can get one [here](https://www.remove.bg/api). * `SAUCENAO_KEY` is your API key for SauceNAO. Register, login, and click [here](https://saucenao.com/user.php?page=search-api) to get one. * `SPOTIFY_KEY` and `SPOTIFY_SECRET` are your key and secret for the Spotify API. You can get one by [following these instructions](https://developer.spotify.com/documentation/web-api). * `THECATAPI_KEY` is your API key for The Cat API. You can get one [here](https://thecatapi.com/). @@ -101,7 +102,7 @@ Only if you want to use the DECTalk command. 18. Start Xiao up! ## Commands -Total: 518 +Total: 519 ### Utility: @@ -458,6 +459,7 @@ Total: 518 * **police-tape:** Draws police tape over an image or a user's avatar. * **rainbow:** Draws a rainbow over an image or a user's avatar. * **rejected:** Draws a "rejected" stamp over an image or a user's avatar. +* **remove-bg:** Removes the background from an image. * **resize:** Draws an image or a user's avatar resized to the size you want. * **rotate:** Draws an image or a user's avatar but rotated a number of degrees. * **sepia:** Draws an image or a user's avatar in sepia. @@ -1223,6 +1225,8 @@ Total: 518 - [PNG Arts](https://www.pngarts.com/) ([Image](https://www.pngarts.com/explore/94078)) * **rejected:** - [Clipart Library](http://clipart-library.com/) ([Image](http://clipart-library.com/clipart/Rejected-Stamp-Transparent.htm)) +* **remove-bg:** + - [remove.bg](https://www.remove.bg/) ([API](https://www.remove.bg/api)) * **sip:** - [CoolClips.com](http://search.coolclips.com/) ([Image](http://search.coolclips.com/m/vector/hand0007/Hands-holding-mug/)) * **sketch:** diff --git a/commands/edit-image/remove-bg.js b/commands/edit-image/remove-bg.js new file mode 100644 index 00000000..df9f3029 --- /dev/null +++ b/commands/edit-image/remove-bg.js @@ -0,0 +1,54 @@ +const Command = require('../../framework/Command'); +const { PermissionFlagsBits } = require('discord.js'); +const request = require('node-superfetch'); +const { REMOVEBG_KEY } = process.env; + +module.exports = class RemoveBgCommand extends Command { + constructor(client) { + super(client, { + name: 'remove-bg', + aliases: ['remove-background'], + group: 'edit-image', + description: 'Removes the background from an image.', + clientPermissions: [PermissionFlagsBits.AttachFiles], + throttling: { + usages: 1, + duration: 120 + }, + credit: [ + { + name: 'remove.bg', + url: 'https://www.remove.bg/', + reason: 'API', + reasonURL: 'https://www.remove.bg/api' + } + ], + args: [ + { + key: 'image', + type: 'image-or-avatar', + default: msg => msg.author.displayAvatarURL({ extension: 'png', size: 2048, forceStatic: true }) + } + ] + }); + } + + async run(msg, { image }) { + try { + const { body } = await request + .post('https://api.remove.bg/v1.0/removebg') + .set({ 'X-Api-Key': REMOVEBG_KEY }) + .send({ + size: 'auto', + image_url: image, + format: 'png' + }); + return msg.say({ files: [{ attachment: body, name: 'remove-bg.png' }] }); + } catch (err) { + if (err.status === 400) return msg.reply('Something is wrong with your image.'); + if (err.status === 402) return msg.reply('Sorry! I\'m out of credits. Try again next month!'); + if (err.status === 429) return msg.reply('Sorry, I\'m overloaded. Try again later!'); + throw err; + } + } +}; diff --git a/package.json b/package.json index 5d081d5c..79731e7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "155.2.3", + "version": "155.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {