mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 04:31:13 +00:00
Add protection against user typos or wrong commands (#880)
* add protection against user typos or wrong commands * black refactoring
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
import sys
|
||||
import difflib
|
||||
import discord
|
||||
import discord_components
|
||||
import pyimgur
|
||||
@@ -15,6 +16,12 @@ gst_bot = discord.ext.commands.Bot(
|
||||
)
|
||||
discord_components.DiscordComponents(gst_bot)
|
||||
|
||||
if cfg.IMGUR_CLIENT_ID == "REPLACE_ME" or cfg.DISCORD_BOT_TOKEN == "REPLACE_ME":
|
||||
print(
|
||||
f"Update IMGUR_CLIENT_ID or DISCORD_BOT_TOKEN or both in { os.path.join('discordbot', 'config_discordot') } \n"
|
||||
)
|
||||
sys.exit()
|
||||
|
||||
|
||||
async def on_ready():
|
||||
print("GST Discord Bot Ready to Gamestonk!")
|
||||
@@ -27,11 +34,28 @@ gst_bot.load_extension("generic_commands")
|
||||
gst_bot.load_extension("economy.economy_menu")
|
||||
gst_bot.load_extension("stocks.dark_pool_shorts.dps_menu")
|
||||
|
||||
if cfg.IMGUR_CLIENT_ID == "REPLACE_ME" or cfg.DISCORD_BOT_TOKEN == "REPLACE_ME":
|
||||
print(
|
||||
f"Update IMGUR_CLIENT_ID or DISCORD_BOT_TOKEN or both in { os.path.join('discordbot', 'config_discordot') } \n"
|
||||
)
|
||||
sys.exit()
|
||||
# Get all command names
|
||||
all_cmds = gst_bot.all_commands.keys()
|
||||
|
||||
# In case the user inserts a wrong command we check for similarity with
|
||||
# available commands, and if there is we suggest one, otherwise we
|
||||
# report list of all commands available
|
||||
|
||||
|
||||
@gst_bot.event
|
||||
async def on_command_error(ctx, error):
|
||||
if isinstance(error, discord.ext.commands.CommandNotFound):
|
||||
cmd = str(error).split('"')[1]
|
||||
similar_cmd = difflib.get_close_matches(cmd, all_cmds, n=1, cutoff=0.7)
|
||||
|
||||
if similar_cmd:
|
||||
error_help = f"Did you mean '**!{similar_cmd[0]}**'?"
|
||||
else:
|
||||
# TODO: This can be improved by triggering help menu
|
||||
error_help = f"**Possible commands are:** {', '.join(all_cmds)}"
|
||||
|
||||
await ctx.send(f"_{error}_\n{error_help}\n")
|
||||
|
||||
|
||||
# Runs the bot
|
||||
gst_bot.run(cfg.DISCORD_BOT_TOKEN)
|
||||
|
||||
Reference in New Issue
Block a user