{"id":582,"date":"2025-01-17T04:37:53","date_gmt":"2025-01-17T04:37:53","guid":{"rendered":"https:\/\/portfolio.wcu.edu\/asmccoy2\/?p=582"},"modified":"2025-04-15T16:36:29","modified_gmt":"2025-04-15T16:36:29","slug":"astrum-cletus-discord-bot","status":"publish","type":"post","link":"https:\/\/portfolio.wcu.edu\/asmccoy2\/2025\/01\/17\/astrum-cletus-discord-bot\/","title":{"rendered":"Astrum Cletus Discord Bot"},"content":{"rendered":"<p>This discord bot named Cletus was a personal project designed and programmed by me for the Astrum discord based nation simulator. This bot could take a set order form from a player, and process the game structures they wished to build and then add the structures they built to a players structures totals, as well as calculating the cost of their projects. This Discord bot was connected to a Google Sheet using Google Developers Google Sheet API. The discord bot was fully programmed in Python 3. This bot is designed to pull from a preset form with edited values, it then breaks the form down into structures, and numbers of structures, and puts those values into 2 sperate index&#8217;s. Using those index&#8217;s, and values pulled from the Astrum Master Sheet the bot can calculate how many structures a player has, how much their order cost, as well as updating their national bank after the daily income is processed. This bot allowed for an in depth, and complex playing system for players to have a unique and free experience on discord, while also taking the work load off the admins when it comes to building orders.<\/p>\n<p>[INSERT VIDEO OF CODE WORKING HERE]<\/p>\n<p>Setting up the Discord Bot.<br \/>\nI set up the discord bot using the following website as a guide.<br \/>\n<a href=\"https:\/\/discordpy.readthedocs.io\/en\/stable\/discord.html\">https:\/\/discordpy.readthedocs.io\/en\/stable\/discord.html\u00a0<\/a><\/p>\n<p>Connecting the Discord Bot to a Google Sheet.<br \/>\nThe first step in this project was setting up a google developer account to get access to the google sheet API.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <a href=\"https:\/\/developers.google.com\/profile\">https:\/\/developers.google.com\/profile<\/a><\/p>\n<p>The second step would be to set up the Google Sheets API.<br \/>\nFor this you will need to go to the google cloud console at <a href=\"https:\/\/console.cloud.google.com\/\">https:\/\/console.cloud.google.com\/\u00a0<\/a><br \/>\nNext you will select your google account.<br \/>\nYou will then go to the project manager, which is the menu next to the google cloud logo.<br \/>\nYou will then make a new project and name this project then hit create.<br \/>\nYou will then select the project once its made and appears in notifications.<br \/>\nYou will then go to the navigation menu, and select API and services, and then hit Enabled APIs and services.<br \/>\nThen hit the Enable APIs and services tab at the top of the screen and search for the google sheets API.<br \/>\nYou will then Enable the google sheets API.<br \/>\nYou will then click the create credentials button.<br \/>\nYou will then select the Application Data, hit next.<br \/>\nYou will then create a service account, hit create and continue, make sure to give this service account the editor role, click continue, then done.<br \/>\nYou will then hit the credentials tab, click the service account you made, click the keys tab, click add key, click create new key, then select JSON as key type, then click create.<br \/>\nYou will then add that JSON file to the project directory for your Python Project.<br \/>\nYou will then need to make a google sheet, and add your service account as an editor to the google sheet.<\/p>\n<p>The third step in this process is to connect the bot python code to your google sheet.<br \/>\nYou will need to install a few packages to interact with the google sheet.<br \/>\nThe command to do this is as follows.<br \/>\npip install &#8211;upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib gspread<\/p>\n<p>Below is the code needed to set up the connection to the google sheet.<\/p>\n<div>\n<div>import gspread<\/div>\n<\/div>\n<div>\n<div>from google.oauth2.service_account import Credentials<\/div>\n<div>scopes = [&#8220;https:\/\/www.googleapis.com\/auth\/spreadsheets&#8221;]<\/div>\n<div>creds = Credentials.from_service_account_file(&#8220;[Inset name of JSON file here&#8221;, scopes=scopes)<\/div>\n<div>c = gspread.authorize(creds)<\/div>\n<div>sheet_id = &#8220;[Put sheet id here]&#8221;<\/div>\n<div>workbook = c.open_by_key(sheet_id)<\/div>\n<div>\n<p>sheet = workbook.worksheet(&#8220;[The sheet you want the bot to edit&#8221;)<\/p>\n<p>Once you do this your python code will be connected to your google sheet.<br \/>\nHere is a in-depth video if you get lost. <a href=\"https:\/\/www.youtube.com\/watch?v=zCEJurLGFRk\">https:\/\/www.youtube.com\/watch?v=zCEJurLGFRk<\/a><\/p>\n<p>Next you will need to connect your code to your discord bot, to do this you will need to the discord.py library<br \/>\npip install Discord.py<br \/>\nYou will also need your discord API Key. At the bottom of your program you will put<\/p>\n<p>Client.run(Put your discord API Key here)<\/p>\n<\/div>\n<div>\n<p>You will also put the following code at the top of your program<\/p>\n<div>\n<div>intents = discord.Intents.default()<\/div>\n<div>intents.message_content = True<\/div>\n<div>client = discord.Client(intents=intents)<\/div>\n<div>@client.event<\/div>\n<div>async def on_ready():<\/div>\n<div>\n<p>\u00a0 \u00a0 print(f&#8217;We have logged in as {client.user}&#8217;)<\/p>\n<p>Here is an Example a fully working discord-python-google sheet bot<\/p>\n<div>\n<div>import discord<\/div>\n<div>import datetime<\/div>\n<div>import pytz<\/div>\n<div>import gspread<\/div>\n<div>import re<\/div>\n<div>import time as t<\/div>\n<div>#Time setup<\/div>\n<div>from discord.ext import commands, tasks<\/div>\n<div>time = datetime.time(hour=17, minute=20, tzinfo=pytz.timezone(&#8220;America\/New_York&#8221;))<\/div>\n<div>#Connection to google sheet<\/div>\n<div>from google.oauth2.service_account import Credentials<\/div>\n<div>scopes = [&#8220;https:\/\/www.googleapis.com\/auth\/spreadsheets&#8221;]<\/div>\n<div>creds = Credentials.from_service_account_file(&#8220;[JSON FILE NAME HERE]&#8221;, scopes=scopes)<\/div>\n<div>c = gspread.authorize(creds)<\/div>\n<div>sheet_id = &#8220;[SHEET ID HERE]&#8221;<\/div>\n<div>workbook = c.open_by_key(sheet_id)<\/div>\n<div>sheet = workbook.worksheet(&#8220;Sheet1&#8221;)<\/div>\n<div>#Commands sent by user bot returns values test<\/div>\n<div>intents = discord.Intents.default()<\/div>\n<div>intents.message_content = True<\/div>\n<div>client = discord.Client(intents=intents)<\/div>\n<div>@client.event<\/div>\n<div>async def on_ready():<\/div>\n<div>\u00a0 \u00a0 print(f&#8217;We have logged in as {client.user}&#8217;)<\/div>\n<div>@client.event<\/div>\n<div>async def on_message(message):<\/div>\n<div>\u00a0 \u00a0 if message.author == client.user:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 return<\/div>\n<div>\u00a0 \u00a0 if message.content.startswith(&#8216;$hello&#8217;):<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 await message.channel.send(&#8216;Hello!&#8217;)<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 elif message.content.startswith(&#8216;$Google&#8217;):<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 value = sheet.acell(&#8220;B4&#8221;).value<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 await message.channel.send(f'{value}&#8217;)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 await message.channel.send()<\/div>\n<div>#Daily Loop Test<\/div>\n<div>goodNightTime = datetime.time(hour=17, minute=20, second=00) #Create the time on which the task should always run<\/div>\n<\/div>\n<\/div>\n<div>\n<p>Client.run(Put your discord API Key here)<\/p>\n<p>Once you do all of this you should have a functioning discord to google sheets bot.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This discord bot named Cletus was a personal project designed and programmed by me for the Astrum discord based nation simulator. This bot could take a set order form from a player, and process the game structures they wished to build and then add the structures they built to a players structures totals, as well [&hellip;]<\/p>\n","protected":false},"author":1963,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[17],"tags":[],"class_list":["post-582","post","type-post","status-publish","format-standard","hentry","category-personal-projects"],"_links":{"self":[{"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/posts\/582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/users\/1963"}],"replies":[{"embeddable":true,"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/comments?post=582"}],"version-history":[{"count":2,"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/posts\/582\/revisions"}],"predecessor-version":[{"id":586,"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/posts\/582\/revisions\/586"}],"wp:attachment":[{"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/media?parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/categories?post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/portfolio.wcu.edu\/asmccoy2\/wp-json\/wp\/v2\/tags?post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}