Random: Various updates and improvements.

This commit is contained in:
Krytarik Raido 2021-12-04 04:04:04 +01:00
parent 2dfd320ddc
commit 6c755906d7
7 changed files with 101 additions and 106 deletions

50
Random/README.rst Normal file
View File

@ -0,0 +1,50 @@
.. _plugin-Random:
Documentation for the Random plugin for Supybot
===============================================
Purpose
-------
This plugin returns a random phrase or number out of a given
set of phrases or range of numbers.
Usage
-----
Return a random phrase or number out of a given
set of phrases or range of numbers.
.. _commands-Random:
Commands
--------
.. _command-random-random:
random <phrase1> <phrase2> ...<phraseN>
Picks a phrase at random. Use quotes to enclose multiple words in a phrase.
.. _command-random-randrange:
randrange <min> <max>
Picks a random number in range <min>-<max>
.. _command-random-seed:
seed [<number>]
Seeds the random number generator with the given <number>, which can either be an integer or a floating-point number.
When no argument is given, the random number generator is seeded with the current time.
.. _conf-Random:
Configuration
-------------
.. _conf-supybot.plugins.Random.public:
supybot.plugins.Random.public
This config variable defaults to "True", is not network-specific, and is not channel-specific.
Determines whether this plugin is publicly visible.

View File

@ -1 +0,0 @@
Insert a description of your plugin here, with any notes, etc. about using it.

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com> # Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -25,12 +26,11 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
""" """
Add a description of the plugin (to be presented to the user inside the wizard) This plugin returns a random phrase or number out of a given
here. This should describe *what* the plugin does. set of phrases or range of numbers.
""" """
import supybot import supybot
@ -38,29 +38,31 @@ import supybot.world as world
# Use this for the version of this plugin. You may wish to put a CVS keyword # Use this for the version of this plugin. You may wish to put a CVS keyword
# in here if you're keeping the plugin in CVS or some similar system. # in here if you're keeping the plugin in CVS or some similar system.
__version__ = "" __version__ = "1.0.0"
# XXX Replace this with an appropriate author or supybot.Author instance. # XXX Replace this with an appropriate author or supybot.Author instance.
__author__ = supybot.authors.unknown __author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@gmail.com")
# This is a dictionary mapping supybot.Author instances to lists of # This is a dictionary mapping supybot.Author instances to lists of
# contributions. # contributions.
__contributors__ = {} __contributors__ = {
supybot.Author("Terence Simpson", "tsimpson", "tsimpson@ubuntu.com"): ['Original Author']
}
# This is a url where the most recent plugin package can be downloaded. # This is a url where the most recent plugin package can be downloaded.
__url__ = '' # 'http://supybot.com/Members/yourname/Random/download' __url__ = 'https://launchpad.net/ubuntu-bots'
import config from . import config
import plugin from . import plugin
reload(plugin) # In case we're being reloaded. from importlib import reload
# In case we're being reloaded.
reload(config)
reload(plugin)
# Add more reloads here if you add third-party modules and want them to be # Add more reloads here if you add third-party modules and want them to be
# reloaded when this plugin is reloaded. Don't forget to import them as well! # reloaded when this plugin is reloaded. Don't forget to import them as well!
if world.testing: if world.testing:
import test from . import test
Class = plugin.Class Class = plugin.Class
configure = config.configure configure = config.configure
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com> # Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -25,12 +26,12 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import supybot.conf as conf import supybot.conf as conf
import supybot.registry as registry import supybot.registry as registry
def configure(advanced): def configure(advanced):
# This will be called by supybot to configure this module. advanced is # This will be called by supybot to configure this module. advanced is
# a bool that specifies whether the user identified himself as an advanced # a bool that specifies whether the user identified himself as an advanced
@ -41,9 +42,7 @@ def configure(advanced):
Random = conf.registerPlugin('Random') Random = conf.registerPlugin('Random')
# This is where your configuration variables (if any) should go. For example: # This is where your configuration variables (if any) should go. For example:
# conf.registerGlobalValue(Random, 'someConfigVariableName', # conf.registerGlobalValue(Random, 'someConfigVariableName',
# registry.Boolean(False, """Help for someConfigVariableName.""")) # registry.Boolean(False, """Help for someConfigVariableName."""))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com> # Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -25,38 +26,23 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import supybot.utils as utils
from supybot.commands import * from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
import supybot.ircdb as ircdb
import random as rdm import random as rdm
import string
import time import time
class Random(callbacks.Plugin): class Random(callbacks.Plugin):
"""Add the help for "@plugin help Random" here """Return a random phrase or number out of a given
This should describe *how* to use this plugin.""" set of phrases or range of numbers."""
threaded = True threaded = True
def __init__(self, irc): def __init__(self, irc):
callbacks.Plugin.__init__(self, irc) self.__parent = super(Random, self)
self.__parent.__init__(irc)
self.rdm = rdm.Random(time.time()) self.rdm = rdm.Random(time.time())
self.pwchars = string.ascii_letters + string.digits + string.punctuation
def _seed(self, num=None):
if num is None:
num = time.time()
self.rdm.seed(num)
def makePassword(self, pwlen=16):
self._seed()
return ''.join(self.rdm.sample(self.pwchars, pwlen))
def seed(self, irc, msg, args, num): def seed(self, irc, msg, args, num):
"""[<number>] """[<number>]
@ -65,80 +51,38 @@ class Random(callbacks.Plugin):
either be an integer or a floating-point number. When no argument is either be an integer or a floating-point number. When no argument is
given, the random number generator is seeded with the current time. given, the random number generator is seeded with the current time.
""" """
self._seed(num) if num is None:
irc.replySuccess() num = time.time()
self.rdm.seed(num)
irc.replySuccess()
seed = wrap(seed, [optional(first('long', 'float'))]) seed = wrap(seed, [optional(first('long', 'float'))])
def random(self, irc, msg, args, words): def random(self, irc, msg, args, phrases):
"""word1 word2 ...wordN """<phrase1> <phrase2> ...<phraseN>
Picks a word at random.
Picks a phrase at random. Use quotes to enclose multiple words
in a phrase.
""" """
wordl = [x for x in words.split(' ') if x] if len(phrases) == 1:
wordl = [_ for _ in wordl if not ('tsimpson',).__contains__(_.lower())] irc.reply(phrases[0]);
if len(wordl) == 1:
irc.reply(wordl[0]);
return return
if self.rdm.randrange(0, 2): if self.rdm.randrange(0, 2):
self.rdm.seed(time.time()) self.rdm.seed(time.time())
irc.reply(self.rdm.choice(wordl)) irc.reply(self.rdm.choice(phrases))
random = wrap(random, ['text']) random = wrap(random, [many('something')])
def randrange(self, irc, msg, args, min, max): def randrange(self, irc, msg, args, minn, maxn):
"""<min> <max> """<min> <max>
Picks a random number in range <min>-<max> Picks a random number in range <min>-<max>
""" """
if max <= min: if maxn <= minn:
irc.error("<min> must be less than <max>") irc.error("<min> must be less than <max>")
return return
max += 1 irc.reply(str(rdm.randrange(minn, maxn+1)))
irc.reply(str(rdm.randrange(min, max))) randrange = wrap(randrange, ['nonNegativeInt', 'nonNegativeInt'])
randrange = wrap(randrange, ["nonNegativeInt", "nonNegativeInt"])
def adduser(self, irc, msg, args, name, hostmask):
"""<name> [<hostmask>]
Registers <name> with the bot and, optionally, assigns the given
<hostmask> to the new account.
"""
# This try-block checks if the user calling this command is an admin (or owner)
try:
u = ircdb.users.getUser(msg.prefix)
if not u._checkCapability('admin'):
irc.errorNoCapability('admin', Raise=True)
except KeyError:
pass
if ircutils.isUserHostmask(name):
irc.errorInvalid('username', name, "Hostmasks are not valid usernames", Raise=True)
# This try-block checks if the username is already registered
try:
ircdb.users.getUserId(name)
irc.error("That name is already registered", Raise=True)
except KeyError:
pass
if hostmask:
# This try-block checks if the hostmask given is already assigned to another user
try:
u = ircdb.users.getUser(hostmask)
irc.error("That hostmask is already registered to %s" % u.name)
return
except KeyError:
pass
user = ircdb.users.newUser()
user.name = name
user.setPassword(self.makePassword())
if hostmask:
user.addHostmask(hostmask)
ircdb.users.setUser(user)
irc.replySuccess()
# adduser = wrap(adduser, ['something', additional('hostmask')])
Class = Random Class = Random
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2010, Terence Simpson <tsimpson@ubuntu.com> # Copyright (c) 2010, Terence Simpson
# Copyright (c) 2021, Krytarik Raido
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -25,13 +26,9 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
from supybot.test import * from supybot.test import *
class RandomTestCase(PluginTestCase): class RandomTestCase(PluginTestCase):
plugins = ('Random',) plugins = ('Random',)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -17,3 +17,7 @@ PackageInfo
=========== ===========
apt-file apt-file
python3-apt python3-apt
Random
======
(none)