-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmassdm.py
58 lines (48 loc) · 1.94 KB
/
massdm.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
import discord
import logging
from discord.ext import commands
from safemodels import SafeGuild, SafeMember, SafeRole
class MassDM(commands.Cog):
"""Send a direct message to all members of the specified Role."""
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=["mdm"])
@commands.guild_only()
@commands.has_permissions(manage_guild=True)
async def massdm(
self, ctx: commands.Context, role: discord.Role, *, message: str
) -> None:
"""Sends a DM to all Members with the given Role.
Allows for the following customizations:
`{member}` is the member being messaged
`{role}` is the role through which they are being messaged
`{server}` is the server through which they are being messaged
`{sender}` is you, the person sending the message
"""
try:
await ctx.message.delete()
except discord.Forbidden:
log.warning("Failed to delete command message: insufficient permissions")
except discord.DiscordException:
log.warning("Failed to delete command message")
for member in [m for m in role.members if not m.bot]:
try:
await member.send(
message.format(
member=SafeMember(member),
role=SafeRole(role),
server=SafeGuild(ctx.guild),
guild=SafeGuild(ctx.guild),
sender=SafeMember(ctx.author),
)
)
except discord.Forbidden:
log.warning(
f"Failed to DM user {member} (ID {member.id}): insufficient permissions"
)
continue
except discord.DiscordException:
log.warning(f"Failed to DM user {member} (ID {member.id})")
continue
def setup(bot):
bot.add_cog(MassDM(bot))