Code Monkey home page Code Monkey logo

discord-medivia-pybot's Introduction

Discord Medivia Python Bot

A Discord bot written in Python for the great MMORPG we all know and love named Medivia (Based on Tibia).

This discord bot uses Rapptz's discord.py.

This bot is used to show what players are online in Medivia based on lists of players you care about.

Table of Contents

How it Works

This discord bot has one cog which contains all the code for the medivia commands.

main.py

This is the main entrypoint of the bot.

This file contains the following:

  • Imports the token the bot will use to authenticate to discord
  • Cycles through the bot's statuses (what game it shows the bot is playing in discord)
  • Prints the bot is ready when the python main.py is run
    • 'RunningnnBot is ready ...'.
  • Loads all the cogs found in the 'cogs' folder

cogs/medivia.py

This file has all the commands the bot provides for Medivia.

  • get_lists
  • new_list
  • remove_list
  • add_member
  • get_member
  • remove_member

Lastly, there is a @tasks.loop which is set to run every 60 seconds which is the main workhorse of the bot. This loop runs the medivia_online function which pulls the online players from the Medivia website and compares that lists to the lists created for your discord via the above commands.

If there are any updates, it will update the channel associated with the list being checked.

config Directory

When the bot is run and the Medivia discord commands are run, the results of the lists/members added/removed are stored in the config directory.

{guild}.json

These files will be named after the discord server. The contents of the file look like this:

{
    "Bullet Club": {
        "id": 00000000,                     # the ID of the discord server this json file is for
        "channels": {
            "Public Enemy": {
                "id": "00000000",           # the ID of the channel to send to
                "members": [
                    "Black Panther",
                    "Orall",
                    "Alf",
                    "Tookas Cryx"
            ]},
            "Watch the Throne": {
                "id": "00000000",           # the ID of the channel to send to
                "members": [
                    "Etrius",
                    "Sudden'slayer",
                    "Hasley",
            ]}
        }
    }
}

{guild}_saved.json

Secondly, when the Medivia website is checked for online users, a file is updated with the latest online members for each list. The file is named after the discord server plus _saved. This is used so every minute the Medivia online list is checked that it won't update each discord channel each time. It will verify the last online list and only update if there are changes from this list.

Running the Bot

Install the required python packages:

pip install beautifulsoup4 discord.py

Add your token as an environmental variable

export TOKEN='xxx'

Run the python app

python main.py

Run via Docker

From the root directory of this repo, run the following commands:

To run in Alpine:

docker build -t discord-medivia-pybot -f Dockerfile-alpine .
docker run -d --name discord-medivia-pybot discord-medivia-pybot

To run in Ubuntu:

docker build -t discord-medivia-pybot -f Dockerfile-ubuntu .
docker run -d --name discord-medivia-pybot discord-medivia-pybot

Run the following command to check logs:

docker logs discord-medivia-pybot

Discord Commands

Command prefix:

med.

Commands:

All commands are specific to the discord server the command is ran against.

  • Lists
    • get_lists
      • Description:
        • Returns all the list for this discord server
      • Parameters:
        • This command does not have any parameters
      • Example:
        • med.get_lists
    • new_list
      • Description:
        • Creates a new list
      • Parameters:
        • listName:
          • The name to give the list that will be created
        • channelId:
          • The ID of the channel the list should be posted to
      • Example:
        • med.new_lists Friends 1234567
        • med.new_lists "Watch the Throne" 1234567
    • remove_list
      • Description:
        • Removes the specified list
      • Parameters:
        • listName
          • The name of the list to remove
      • Example:
        • med.remove_list Friends
        • med.remove_list "Watch the Throne"
  • Members
    • add_member
      • Description:
        • Adds the specified member(s) to the specified list
      • Parameters:
        • listName
          • The name of the list to add the member(s) to
        • *members
          • The member(s) to add to the list
      • Example:
        • med.add_member Friends "Beardtopia"
        • med.add_member "Inner Circle" "Beardtopia" "Silenus" "Listen Lady"
    • remove_member
      • Description:
        • Removes the specified member(s) from the specified list
      • Parameters:
        • listName
          • The name of the list to remove the member(s) to
        • *members
          • The member(s) to remove from the list
      • Example:
        • med.remove_member Friends "Beardtopia"
        • med.remove_member "Inner Circle" "Beardtopia" "Silenus" "Listen Lady"
    • get_members
      • Description:
        • Returns all the members of the specified list
      • Parameters:
        • listName
          • The name of the list to return member(s) from
      • Example:
        • med.remove_member Friends
        • med.remove_member "Inner Circle"

discord-medivia-pybot's People

Contributors

midacts avatar

Watchers

 avatar

discord-medivia-pybot's Issues

Not Removing Offline Players

Python (and Packages) Version

# python --version
Python 3.8.2

# pip list
Package        Version
-------------- -------
aiohttp        3.6.2
async-timeout  3.0.1
attrs          19.3.0
beautifulsoup4 4.8.2
chardet        3.0.4
discord.py     1.3.2
idna           2.9
multidict      4.7.5
pip            20.0.2
setuptools     45.2.0
soupsieve      2.0
websockets     8.1
wheel          0.34.2
yarl           1.4.2

Affected Resource(s)

  • medivia_online

Expected Behavior

Offline members should be removed from the discord channels

Actual Behavior

Players are not removed from discord channels if they are the only person on the list and they log off.

Add Support for an "All" List

Description

Add a default "All" list which shows all the members of all the lists (of the specific discord server) into one list/channel.

New or Affected Resource(s)

  • medivia_online

Add Support for Different Realms

Description

A support for different realms.
Currently, only pendulum is supported

New or Affected Resource(s)

  • cogs/medivia.py

Bot Freezes at Random

Python (and Packages) Version

# python --version
Python 3.8.2

# pip list
Package        Version
-------------- -------
aiohttp        3.6.2
async-timeout  3.0.1
attrs          19.3.0
beautifulsoup4 4.8.2
chardet        3.0.4
discord.py     1.3.2
idna           2.9
multidict      4.7.5
pip            20.0.2
setuptools     45.2.0
soupsieve      2.0
websockets     8.1
wheel          0.34.2
yarl           1.4.2

Affected Resource(s)

  • get_realm

Expected Behavior

The bot should remain running as expected until stopped.

Actual Behavior

The bot freezes up and stops running.

Steps to Reproduce

Run the bot and it will freeze at random.

  1. python main.py

Important Facts

I asked this question in the discord.py discord channel and gogurt ktinky (gogurt enjoyer
#8035) showed me that I should make two changes:

  • Use aiohttp instead of request/urllib
  • Make sure I am closing all the files the code is opening during the json.load calls, etc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.