Twentysix26 / x26-Cogs

General purpose cogs for Red V3
GNU General Public License v3.0
41 stars 29 forks source link

[Feature request] Random responses for warden #17

Closed stephanrenggli closed 3 years ago

stephanrenggli commented 3 years ago

Cog

Describe the feature you'd like to see Add multiple possibilities for all message sending actions such as send-in-channel, dm-user, send-dm. Like this warden could randomly choose one possible response and send it.

Maybe even an optional weight/probability parameter so you can control which message is more likely to be chosen.

As the configuration is in yaml something like this could work.

Without weight.

rank: 2
name: random-greeting
event: on-message
if:
  - message-matches-any: ["hello"]
do:
  - send-in-channel:
    - message: "hello $user_mention"
    - message: "greetings $user_mention"
    - message: "howdy $user_mention"

With optional weight parameter.

rank: 2
name: random-weighted-greeting
event: on-message
if:
  - message-matches-any: ["hello"]
do:
  - send-in-channel:
    - message: "hello $user_mention"
      weight: 80
    - message: "greetings $user_mention"
      weight: 10
    - message: "howdy $user_mention"
      weight: 10
stephanrenggli commented 3 years ago

I was able to implement this using the random.choices function: Not pretty but the basics work :)

elif action == Action.SendInChannel:
  weights = []
  for e in value:
    weights.append(e['weight'])
  chosen_message = random.choices(value, weights=weights, k=1)
  chosen_message = chosen_message[0]['message']
  text = Template(chosen_message).safe_substitute(templates_vars)
  last_sent_message = await channel.send(text, allowed_mentions=ALLOW_ALL_MENTIONS)

Would require some additional checks to make sure every message has a weight or alternatively calculate weight based on the other specified weights.

Twentysix26 commented 3 years ago

Hi, this suggestion has been fully added in 1.6 with the action var-assign-random