Skip to content

An example of a bot that interacts with MediaWiki API from a Discord client.

Notifications You must be signed in to change notification settings

eglopez07/Discord-MediaWiki-API-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Discord-MediaWiki-API-Bot

An example of a bot that interacts with MediaWiki API from a Discord client.

This project was designed to be a quick proof-of-concept for a program that receives input via the Discord application and produces an output depending on what the input was. We leverage the Discord.py library to simplify the reading of inputs from the Discord app, as well as the mwclient library to simplify making API calls. There are simple commands to test that the program is receiving input correctly, as well as a more complex command that gets data from the Mizuumi Wiki website using the MediaWiki API.

Basic Functions

The bot will ignore any messages that do not begin with a $ character and continuously wait until it sees such a message. When it does, it reads the string following the $ to determine which command it is and executes the associated action. After this, it returns to waiting for another command. For example, if one types $hello then the bot will respond:

image

If one types $length the bot will return the character length of the message:

image

These simple commands do not have additional arguments; the bot takes in the entire message containing the command as the sole argument.

Cargo Queries

The primary function of the bot is to run a query on a database for a MediaWiki site. This is done with the help of MediaWiki's Cargo extension. This extension stores data in tables and also makes it available to external users through a specific API action, "cargoquery". The specific MediaWiki site we are targeting is predefined in a variable. When the bot reads the command $fd s (where s is a string of characters) it will run the "cargoquery" API action on the specified site. The various API action parameters are pre-maintained to retrieve a particular value on the table, with only the s argument being used to help filter the result.

As an example, if we enter the command $fd na_5a then the bot will read that message and turn it into a query equivalent to the following wikitext:

{{#cargo_query:tables=UNICLR_MoveData|fields=UNICLR_MoveData.moveId, UNICLR_MoveData.damage|where=UNICLR_MoveData.moveId="na_5a"}}

Within the bot program, the API action will return the result in the form of an OrderedDict structure. We can then output the value back to Discord, as shown below:

image

To compare, this link shows the result of the exact same query as above. As of 07/03/2023, the results should be identical.

image

About

An example of a bot that interacts with MediaWiki API from a Discord client.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages