Skip to content

sachinlim/discord-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Bot

Discord bot designed to keep servers clean with shortening long links and adding commands to help reduce search times.

It is made with Discord.py API wrapper, and with how the API wrapper works, it allows the ability to store features for the bot in a separate class with the use of the Discord.py cogs feature. The features of this Bot can be found in the cogs directory.

Additional Bots

As this was the first venture with creating bots, other Discord bots have also been created, which adopt certain cogs and include new ones. The other bots seeing the most use is the hwsuk-bot, which was created for a community of 19,000 hardware traders on Reddit, located in the UK. The bot is available to a growing community of 2000 users that are involved in the community's Discord server.

Key Features

  • eBay sold price scraper for used items
  • op.gg Live Game scraper
  • Weather updates
  • Link shortener for eBay and Amazon

These features found in the cogs directory can be extracted and implemented as a standalone Python script, as long as the relevant modules are installed. This allows for the use of Discord bots to act as a wrapper that impements many projects as commands.

Prerequisites

The Bot is created using Python 3.10 and requires a few modules to be installed.

Installing using pip

# Installing the entire requirements.txt file
pip install -r requirements.txt

# Manual installation of modules
pip install discord selenium requests beautifulsoup4

In order for the op.gg Live Game scraper to work, ChromeDriver needs to be downloaded and placed in the same directory as the dukies-bot.py file. There should be another file called actives.py, where the bot can get private variables from, such as the token ID for the bot or the channel IDs for the hourly weather updates.

Staying online

To host this on DigitalOcean (or any cloud provider), GNU screen has been used to always keep the bot online. If you run the bot script with python3 dukies-bot.py, it will no longer be online after you close the console/terminal. To prevent this and always stay online, screen can be used.

  • Install screen pip install screen
  • Write screen to open it up
  • Read the message and press enter
  • Navigate to your script and run it with python3 dukies-bot.py
  • Close the window or exit screen with [CTRL] + A + D

One thing I discovered was that the basic Droplet on DigitalOcean was not good enough for hwsuk-bot because the screen instance would kill itself after a few days. After looking at the activity history on DigitalOcean, there were random CPU spikes, over 70% memory utilisation, and high disk usage. This made me upgrade one tier up, to a Droplet with 1GB RAM but still with one CPU core. There have been no issues so far.

Screenshots

eBay scraper

This is the !search command that provides information from eBay's advance search by scraping the information available. It uses Beautiful Soup to scrape the sold prices while using the filters: Exact words, any order, Sold listings, Used, UK only. The prices are stored in a list before the Bot is able to present information to the user regarding the value of the item in the current market.

Sometimes, the description of a listing can tell potential buyers that an item is faulty. Other times, the items are sold in bulk in a single listing, such as "5x Intel i5-7400", meaning the price for the listing is 5 times higher than if it was a single item being sold. To try and avoid abnormalities like these, data trimming is done by 15% for the lowest and highest prices, which reduces the total list of items down to 70%. This should mean that most of the outliers are removed.

In the event that there is a typo in the search term or the item does not exist, an error message is sent, like the one above.

op.gg Live Game scraper

This scraper was made using Selenium to have information scraped. There are two versions of this: one using the command !ig which only provides the URL for the specified account name, while !ig2 provides the scraped information.

The UI for op.gg's Live Game looks like this:

As op.gg uses Javascript to display information from the Riot API, it requires the use of Javascript to be enabed. This meant that Beautiful Soup would not work, therefore, Selenium was used alongside ChromeDriver.

Discord's embedded messages have a limit of only displaying 3 rows, therefore, only 3 sets of information could be included. This is fine, as the useful information that is needed are: Champion (character) name, rank, and win rate.

Weather

This below is the hourly weather update. It can also be called manually with the !weather command with the location also entered. The JSON data for the relevant city is called using the Weather API from OpenWeatherMap, and formatted to fit Discord's embedded messages.

Link Shortener

This is available for UK eBay and Amazon links. These websites only need the item number to show the correct listing but there are many occasions where other information is added onto the website URL.

The original URL could be something like:

About

A Discord Bot that can be used to help manage and add functionality to Discord servers

Topics

Resources

Stars

Watchers

Forks

Languages