So, I currently have a command I created that will go through a text file and tell you if the content in your message is found in the txt file.
So people will do !stake (username), and it the bot will go through the text document and tell them if their username shows on the list or not.
However, I need help on how to go about getting the below command to read from a google sheet, rather than a txt document. All the content in the google sheet is in one row and there is a username in each cell.
Ex: Row A: 1- User1 2- User2 3- User3 4- User4 etc.
[The command I am working with.]
@client.command()
async def check(ctx, arg):
user = arg
if user in open("affiliates.txt").read():
embed = discord.Embed(title=f"**Woohoo! @{ctx.author.name}**")
embed.add_field(name=f"Your username is on the list", value=":white_check_mark:", inline=False)
await ctx.send(embed=embed)
await asyncio.sleep(10)
await ctx.channel.purge(limit=2)
else:
embed = discord.Embed(title=f"**Woops! @{ctx.author.name}**")
embed.add_field(name=f"Your username is not on the list!", value=":x:", inline=False)
await ctx.send(embed=embed)
await asyncio.sleep(10)
await ctx.channel.purge(limit=2)
Any help would be much appreciated!
I figured this out. I feel silly lol. Here are some instructions of how I achieved my goal.
|
Create a New Project on Google Cloud Go to the following link and create a new project https://console.cloud.google.com/.
Adding the API’s
Now we need to add the following API’s :
– google drive
– google sheets
Once you add the google drive API it will ask you to create credentials. Follow the steps and you should see a JSON file gets downloaded. SAVE THIS FILE and rename it to "credentials" and add it to the directory for your python source file.
Sharing the Sheet
Open up the json file that was downloaded earlier and find the client email. Copy the email and share your google sheet with that email address.
Install Modules
– gspread
– oauth2client
Once you have all of that setup and good to go, you can refer to my code below and reconstruct it as needed.
import discord, json, asyncio, gspread
from discord.ext import commands
from discord.ext.commands import cooldown, BucketType
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
cli = gspread.authorize(creds)
sheet = cli.open("<ENTER GOOGLE SHEET NAME>").sheet1 # Open the spreadsheet
data = sheet.get_all_records() # Get a list of all records
row = sheet.row_values(1) # Get a specific row
col = sheet.col_values(1) # Get a specific column
@client.command()
@commands.cooldown(1, 60, commands.BucketType.user) #Cooldown timer on command
async def stake(ctx, arg):
user = arg
cli.open("<ENTER GOOGLE SHEET NAME>").sheet1
if user in col:
embed = discord.Embed(title=f"**Woohoo! @{ctx.author.name}**")
embed.add_field(name=f"Your username is on the list!", value=":white_check_mark:", inline=False)
await ctx.send(embed=embed)
await asyncio.sleep(10) ## Timer for purge
await ctx.channel.purge(limit=2) ## Deletes the users command message and the bots response after 10 seconds.
print(f" Affiliate name {arg} has been found!")
else:
embed = discord.Embed(title=f"**Woops! @{ctx.author.name}!**")
embed.add_field(name=f"Your username is not on the list!!", value=":x:", inline=False)
await ctx.send(embed=embed)
await asyncio.sleep(10) ## Timer for purge
await ctx.channel.purge(limit=2) ## Deletes the users command message and the bots response after 10 seconds.
print(f" Affiliate name {arg} was not found!")
@stake.error
async def command_name_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f"**@{ctx.author.name} Please wait 60s before using this command again! :warning:**")
await asyncio.sleep(10)
await ctx.channel.purge(limit=2)