Compare commits

..

2 Commits

Author SHA1 Message Date
d8caed033e argparse no longer prints to console 2020-01-29 14:15:43 -05:00
69ee8f69f8 fix unload error on funcs with @rate 2020-01-29 14:06:58 -05:00
7 changed files with 45 additions and 19 deletions

2
bot.py
View File

@ -173,7 +173,7 @@ class Fulvia(irc.IRCClient):
self._hooks.remove(func) self._hooks.remove(func)
if func.rate or func.channel_rate or func.global_rate: if func.rate or func.channel_rate or func.global_rate:
self._times.pop(func) self._times.pop(func.__name__)
if hasattr(func, 'url_callback'): if hasattr(func, 'url_callback'):
for url in func.url_callback: for url in func.url_callback:

View File

@ -3,7 +3,6 @@
ASCII ASCII
""" """
from io import BytesIO from io import BytesIO
import argparse
import requests import requests
from PIL import Image, ImageFont, ImageDraw from PIL import Image, ImageFont, ImageDraw
@ -11,6 +10,7 @@ from PIL import Image, ImageFont, ImageDraw
import numpy as np import numpy as np
import numpngw import numpngw
import tools
import module import module
ASCII_CHARS = "$@%#*+=-:. " ASCII_CHARS = "$@%#*+=-:. "
@ -235,7 +235,7 @@ def ascii(bot, trigger):
""" """
if len(trigger.args) < 2: if len(trigger.args) < 2:
return bot.msg() return bot.msg()
parser = argparse.ArgumentParser(add_help=False) parser = tools.FulviaArgparse()
parser.add_argument("imagePath") parser.add_argument("imagePath")
parser.add_argument("-r", "--reverse", action="store_true", help="Reverse.") parser.add_argument("-r", "--reverse", action="store_true", help="Reverse.")
parser.add_argument("-c", "--color", action="store_true") parser.add_argument("-c", "--color", action="store_true")
@ -243,10 +243,10 @@ def ascii(bot, trigger):
parser.add_argument("-B", "--brail2", action="store_true") parser.add_argument("-B", "--brail2", action="store_true")
parser.add_argument("-a", "--animated", action="store_true") parser.add_argument("-a", "--animated", action="store_true")
parser.add_argument("-h", "--help", action="store_true") parser.add_argument("-h", "--help", action="store_true")
try:
args = parser.parse_args(trigger.args[1:]) args = parser.parse_args(trigger.args[1:])
except Exception as e:
if args.help: return bot.reply(type(e).__name__ + ": " + str(e))
return bot.msg(parser.print_help())
if args.color: if args.color:
args.color = "irc" args.color = "irc"

View File

@ -3,8 +3,8 @@
Resistor color band codes. Resistor color band codes.
""" """
import re import re
import argparse
import tools
from module import commands, example from module import commands, example
suffix = {"k": 1000, "m": 10000000} suffix = {"k": 1000, "m": 10000000}
@ -62,11 +62,14 @@ def resist(bot, trigger):
""" """
if len(trigger.args) < 2: if len(trigger.args) < 2:
return bot.msg("Please specify a value") return bot.msg("Please specify a value")
parser = argparse.ArgumentParser() parser = tools.FulviaArgparse()
parser.add_argument("value", nargs="+") parser.add_argument("value", nargs="+")
parser.add_argument("-r", "--reverse", action="store_true") parser.add_argument("-r", "--reverse", action="store_true")
parser.add_argument("-n", "--num_bands", type=int, choices=[3,4,5,6], default=4) parser.add_argument("-n", "--num_bands", type=int, choices=[3,4,5,6], default=4)
try:
args = parser.parse_args(trigger.args[1:]) args = parser.parse_args(trigger.args[1:])
except Exception as e:
return bot.reply(type(e).__name__ + ": " + str(e))
if args.reverse: # bands-to-value if args.reverse: # bands-to-value
bot.msg(bands_to_value(" ".join(args.value))) bot.msg(bands_to_value(" ".join(args.value)))

View File

@ -4,13 +4,13 @@ When was this user last seen.
""" """
import os import os
import time import time
import argparse
import threading import threading
from datetime import datetime from datetime import datetime
from sqlite3 import OperationalError from sqlite3 import OperationalError
from requests.structures import CaseInsensitiveDict from requests.structures import CaseInsensitiveDict
import tools
import config import config
from tools.time import relativeTime from tools.time import relativeTime
from module import commands, example, hook, require_chanmsg, rate from module import commands, example, hook, require_chanmsg, rate
@ -68,13 +68,16 @@ def seen(bot, trigger):
if len(trigger.args) < 2: if len(trigger.args) < 2:
return bot.reply("Seen who?") return bot.reply("Seen who?")
parser = argparse.ArgumentParser() parser = tools.FulviaArgparse()
parser.add_argument("nick") parser.add_argument("nick")
parser.add_argument("-l", "--last", action="store_true", default=True) parser.add_argument("-l", "--last", action="store_true", default=True)
parser.add_argument("-f", "--first", action="store_true") parser.add_argument("-f", "--first", action="store_true")
parser.add_argument("-m", "--message", action="store_true") parser.add_argument("-m", "--message", action="store_true")
parser.add_argument("-c", "--context", action="store_true") parser.add_argument("-c", "--context", action="store_true")
try:
args = parser.parse_args(trigger.args[1:]) args = parser.parse_args(trigger.args[1:])
except Exception as e:
return bot.reply(type(e).__name__ + ": " + str(e))
if args.nick == bot.nick: if args.nick == bot.nick:
return bot.reply("I'm right here!") return bot.reply("I'm right here!")

View File

@ -5,8 +5,8 @@ All data comes from http://www.rsdb.org/.
""" """
import random import random
import sqlite3 import sqlite3
import argparse
import tools
from module import commands from module import commands
@ -33,11 +33,14 @@ def slur(bot, trigger):
-s, --slur - specifies a particular slur to pick. -s, --slur - specifies a particular slur to pick.
-l, --list - prints a list of available races. -l, --list - prints a list of available races.
""" """
parser = argparse.ArgumentParser() parser = tools.FulviaArgparse()
parser.add_argument("-r", "--race", type=str, nargs='+') parser.add_argument("-r", "--race", type=str, nargs='+')
parser.add_argument("-s", "--slur", type=str) parser.add_argument("-s", "--slur", type=str)
parser.add_argument("-l", "--list", action="store_true") parser.add_argument("-l", "--list", action="store_true")
try:
args = parser.parse_args(trigger.args[1:]) args = parser.parse_args(trigger.args[1:])
except Exception as e:
return bot.reply(type(e).__name__ + ": " + str(e))
if args.list: if args.list:
races = bot.db.execute("SELECT DISTINCT race FROM slur").fetchall() races = bot.db.execute("SELECT DISTINCT race FROM slur").fetchall()

View File

@ -3,10 +3,10 @@
Google translate that shit. Google translate that shit.
""" """
import random import random
import argparse
import requests import requests
import tools
from module import commands, example from module import commands, example
@ -45,11 +45,14 @@ def tr2(bot, trigger):
if len(trigger.args) < 2: if len(trigger.args) < 2:
return bot.reply("Translate what?") return bot.reply("Translate what?")
parser = argparse.ArgumentParser() parser = tools.FulviaArgparse()
parser.add_argument("text", nargs=argparse.REMAINDER) parser.add_argument("text", nargs=argparse.REMAINDER)
parser.add_argument("-i", "--inlang", default="auto") parser.add_argument("-i", "--inlang", default="auto")
parser.add_argument("-o", "--outlang", default="en") parser.add_argument("-o", "--outlang", default="en")
try:
args = parser.parse_args(trigger.args[1:]) args = parser.parse_args(trigger.args[1:])
except Exception as e:
return bot.reply(type(e).__name__ + ": " + str(e))
args.text = " ".join(args.text) args.text = " ".join(args.text)
tr_text, in_lang = translate(args.text, in_lang=args.inlang, tr_text, in_lang = translate(args.text, in_lang=args.inlang,

View File

@ -3,6 +3,7 @@
Some helper functions and other tools. Some helper functions and other tools.
""" """
import re import re
import argparse
import threading import threading
from collections import defaultdict from collections import defaultdict
@ -199,3 +200,16 @@ def getOpSym(level):
return "+" return "+"
else: else:
return " " return " "
class FulviaArgparse(argparse.ArgumentParser):
"""
A custom ArgParser class that raises errors as exceptions rather than
printing them to stderr.
"""
def __init__(self, *args, **kwargs):
kwargs['add_help'] = False
super().__init__(*args, **kwargs)
def error(self, message):
raise argparse.ArgumentError(None, message)