Categorygithub.com/svntax/PlayingCardsBot
modulepackage
0.0.0-20221201073118-5704c6019479
Repository: https://github.com/svntax/playingcardsbot.git
Documentation: pkg.go.dev

# README

Playing Cards Bot

A Discord bot that gives servers a standard 52-card deck of cards for users to play with. Made using DiscordGo.

Commands

CommandDescription
/info, $pcb infoDisplays bot info and a list of all commands.
/draw, $pcb drawDraws a card from the current deck.
/shuffle, $pcb shuffleShuffles the current deck of cards.
/reset-cards, $pcb reset_cardsReplaces the current deck with a brand new, ordered deck of 52 cards.
/set-styleChange the style of the cards. Options are "normal" and "pixel".
(Old) $pcb set_style_normalChanges the art style of the cards to normal.
(Old) $pcb set_style_pixelChanges the art style of the cards to pixel art.
/include-jokersAdd or remove the red & black Joker cards from the deck.
(Old) $pcb include_jokersAdd the red and black Joker cards to the deck.
(Old) $pcb remove_jokersRemove the red and black Joker cards from the deck.
$pcb high_or_lowStarts a game of High or Low.
/quit-game, $pcb quitgameStops any currently running game.

The list of commands can also be found on the live website (https://playing-cards-bot-rvpup.ondigitalocean.app/).

Setup

Go to the live website (https://playing-cards-bot-rvpup.ondigitalocean.app/), and click on "Add to Discord" to be redirected to Discord, log in, then choose a server to add the bot to. Note that you'll need your own Discord server or have access to a server where you have Manage Server permissions.

How To Deploy

To host your own instance of this bot, you'll first need to set up a new application and bot on Discord here: https://discordapp.com/developers/applications/.

After setting up the bot application, click on the Deploy to DigitalOcean Button below.

Deploy to DO

The app uses two environment variables:

BOT_TOKEN is the secret token you can get from your bot application created on Discord. Make sure this token is kept secret.

HOST_URL is the url of where the bot will be hosted (e.g., https://yourcustomdomain.tld). If you have a custom domain, enter it in full here.

To add the bot to Discord servers, you need to generate an OAuth2 link by going to your bot application in the Discord Developer Portal, clicking OAuth2, "bot" for the scope, and checking the following permissions:

  • View Channels
  • Send Messages
  • Embed Links
  • Read Message History
  • Add Reactions

The generated link should have the following format: https://discord.com/api/oauth2/authorize?client_id=<your bot's client id>&permissions=85056&scope=bot

Alternatively, you can just copy-paste the above link and replace <your bot's client id> with the client ID found in General Information.

Once the bot is deployed, feel free to make changes to the frontend, such as replacing the "Add to Discord" button's link with your own bot's.

Development

The backend consists of main.go and the playingcards module for cards functionality (e.g., drawing, shuffling cards).

The frontend, found in /public/, uses plain HTML and CSS and is served by the backend.

The /card_images/ directory contains the playing cards images the bot will use, so you can freely add your own sets of images for the bot to use, but you'll have to update the code to support using more image sets. The two current image sets are by Kenney, which you can find here:

The bot can be hosted locally by running go run main.go -t=<your bot token> -app=<your bot application ID>, but the card images will not display since they won't be reachable within Discord. Without a HOST_URL, the bot will be running on http://localhost:8080 by default.

# Packages

No description provided by the author

# Functions

GetCardPath is specific to how the card images are named.
GetCardURL returns the full url to the image for the given card.
GetServerState looks for the given server and returns it if it exists, or creates a new entry first.
HighOrLowGame starts a new game of High or Low for the given Discord server in the channel the bot responded to.
NewServerState creates a new state struct for the given Discord server.

# Constants

Constants that represent a player's decision in a High or Low game.
Constants for the games supported by the bot.
Constants that represent what card images to use.
Constants that represent what card images to use.
Constants that represent a player's decision in a High or Low game.
Constants for the games supported by the bot.
Constants that represent a player's decision in a High or Low game.

# Variables

Bot token can be passed as a command line argument.
Bot token can be passed as a command line argument.
Bot token can be passed as a command line argument.
Bot token can be passed as a command line argument.

# Structs

GameState represents the current game running in a Discord server.
PlayerState represents a player's state during a game of High or Low.
ServerState holds data on the current state of a Discord server.