How can I use Pandas DataFrame and sent to Discord

Viewed 390

I'm currently developing a Discord Bot that get message(User ID) strings from a Google spreadsheet and returns the corresponding data. But I think the way I am using the DataFrame is not correct, is there any other better way.

import aiohttp
import discord
import asyncio
from discord.ext import commands
from oauth2client.service_account import ServiceAccountCredentials as SAC
import gspread
import pandas as pd

bot = commands.Bot(command_prefix='!')

scope = ['https://spreadsheets.google.com/feeds']
Json = 'xxx.json'
Connect = SAC.from_json_keyfile_name(Json, scope)
GoogleSheets = gspread.authorize(Connect)
sheet = GoogleSheets.open_by_key('key')
Sheets = sheet.sheet1   
df = pd.DataFrame(Sheets.get_all_records())

@bot.event
async def on_ready():
    await bot.wait_until_ready()
    bot.session = aiohttp.ClientSession()
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------------')
    await bot.wait_until_ready()
    bot.session = aiohttp.ClientSession()
    
@bot.command()
async def LList(message, a: str):     

    MIT = await df[df['Discord ID'] == message]['ticket\n(total)'][0]
    await message.send("```\n{}\n You have {} ticket```".format(a, str(MIT)))
       
bot.run('key')

and I got Error here, I don't think there is a way for me to use the local package, but I don't know if there is any other way to do the same thing.

Ignoring exception in command list:
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1625, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1632, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "<ipython-input-42-1c34e43fc9a7>", line 33, in LList
    MIT = await df[df['Discord ID'] == message]['ticket\n(total)'][0]
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\pandas\core\series.py", line 824, in __getitem__
    return self._get_value(key)
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\pandas\core\series.py", line 932, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 0

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\user\anaconda3\envs\user\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 0

Thanks in advance for your Answers.

2 Answers

MIT = await df[df['Discord ID'] == message]['ticket\n(total)'][0]
I'm not sure why you would need await here try removing it even though I'm unsure that would resolve your issue

message is a commands.Context object, so you are probably wanting message.author.id or something. I think nicofrlo is probably right about not needing await there also.

The easiest way to debug this would probably be to use the python terminal to get the desired df and play around with df[df['Discord ID'] == message.author.id]['ticket\n(total)'][0] in the terminal so that you can see what's going on before trying to pass it around to discord.

Related