forked from pckv/pcbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias.py
114 lines (87 loc) · 4.33 KB
/
alias.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
""" Plugin for creating user based alias commands
Commands:
alias
"""
import discord
import bot
import plugins
from pcbot import Config, Annotate, config, utils
client = plugins.client # type: bot.Client
alias_desc = \
"Assign an alias command, where trigger is the command in it's entirety: `{pre}cmd` or `>cmd` or `cmd`.\n" \
"Feel free to use spaces in a **trigger** by *enclosing it with quotes*, like so: `\"{pre}my cmd\"`.\n\n" \
"**Options**:\n" \
"`-anywhere` makes the alias trigger anywhere in a message, and not just the start of a message.\n" \
"`-case-sensitive` ensures that you *need* to follow the same casing.\n" \
"`-delete-message` removes the original message. This option can not be mixed with the `-anywhere` option.\n" \
aliases = Config("user_alias", data={})
@plugins.command(description=alias_desc, pos_check=lambda s: s.startswith("-"))
async def alias(message: discord.Message, *options: str.lower, trigger: str, text: Annotate.Content):
""" Assign an alias. Description is defined in alias_desc. """
anywhere = "-anywhere" in options
case_sensitive = "-case-sensitive" in options
delete_message = not anywhere and "-delete-message" in options
if str(message.author.id) not in aliases.data:
aliases.data[str(message.author.id)] = {}
# Set options
aliases.data[str(message.author.id)][trigger if case_sensitive else trigger.lower()] = {
"text": text,
"anywhere": anywhere,
"case_sensitive": case_sensitive,
"delete_message": delete_message}
await aliases.asyncsave()
m = "**Alias assigned.** Type `{}`{} to trigger the alias."
await client.say(message, m.format(trigger, " anywhere in a message" if anywhere else ""))
@alias.command(name="list")
async def list_aliases(message: discord.Message, member: discord.Member = Annotate.Self):
""" List all user's aliases. """
assert str(message.author.id) in aliases.data, f"**{member.display_name} has no aliases.**"
# The user is registered so they must have aliases and we display them
format_aliases = ", ".join(aliases.data[str(member.id)].keys())
await client.say(message, f"**Aliases for {member.display_name}:**```\n{format_aliases}```\n")
@alias.command()
async def remove(message: discord.Message, trigger: Annotate.Content):
""" Remove user alias with the specified trigger. Use `*` to delete all. """
if trigger == "*":
aliases.data[str(message.author.id)] = {}
await aliases.asyncsave()
await client.say(message, "**Removed all aliases.**")
# Check if the trigger is in the would be list (basically checks if trigger is in [] if user is not registered)
assert trigger in aliases.data.get(str(message.author.id), []), \
f"**Alias `{trigger}` has never been set. Check `{list_aliases.cmd.name_prefix(message.guild)}`.**"
# Trigger is an assigned alias, remove it
aliases.data[str(message.author.id)].pop(trigger)
await aliases.asyncsave()
await client.say(message, f"**Alias `{trigger}` removed.**")
@plugins.event()
async def on_message(message: discord.Message):
success = False
# User alias check
if str(message.author.id) in aliases.data:
user_aliases = aliases.data[str(message.author.id)]
# Check any aliases
for name, command in user_aliases.items():
execute = False
msg = message.content
args = utils.split(msg)
if not command.get("case_sensitive", False):
msg = msg.lower()
if command.get("anywhere", False):
if name in msg:
execute = True
else:
if name == args[0]:
execute = True
if execute:
if command.get("delete_message", False):
if message.channel.permissions_for(message.guild.me).manage_messages:
client.loop.create_task(client.delete_message(message))
text = command["text"]
pre = config.guild_command_prefix(message.guild)
# Execute the command if it is one
if text.startswith(pre):
await plugins.execute(utils.split(text)[0][len(pre):], message)
else:
await client.say(message, text)
success = True
return success