-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreeting_machine.py
116 lines (96 loc) · 5.54 KB
/
greeting_machine.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
115
116
import discord
import os
import json
import asyncio
import re
from discord.ext import commands
from discord import PermissionOverwrite, Permissions
intents = discord.Intents.default()
intents.guilds = True
intents.messages = True
intents.message_content = True
intents.members = True
# git_ignore the JSON file for security, change this line if renamed
with open('config.json') as config_file:
config = json.load(config_file)
#Change the const to whatever, change the config string to match JSON
CARELESSLOVE_ID = config['carelesslove_id']
BGR_WELCOME = config['bgr_welcome']
CL_CHATTER = config['cl_chatter']
CL_PRIVATE = config['cl_private']
bot = commands.Bot(command_prefix='$', intents=intents)
pronoun_roles = []
guild = None
def role_numbers_check(message):
# we use "/" character here, because pronouns will always be "x/y" format
pronoun_roles = [role for role in ctx.guild.roles if '/' in role.name]
return all(i.isdigit() and 0 < int(i) <= len(pronoun_roles) for i in message.content.split(' '))
@bot.command()
async def intro(ctx):
await ctx.send(f"hello, {ctx.guild.name}. i am the greeting machine. i will greet users when they join this server.\n\ni will also help users add or remove pronoun roles. to do this, begin by typing \"``$pronouns``\"\n\n**see below for a full list of greeting machine commands**:\n\n``$intro`` - repeats this introduction\n``$pronouns`` - prints a full list of this server's roles that are associated with preferred pronouns\n``$assign`` - if you already know the number associated with your preferred pronoun as it is printed with the \"``$pronouns``\" command, you can use \"``$assign``\" instead. this skips the step of printing the list, and can look like \"``$assign 1``\", or \"``$assign 1 and 2``\", or \"``$assign 1 2 & 3``\", and so on.\n\nif you do not see your preferred pronouns in our listing, please either tag or direct message the administrator.")
@bot.command()
async def pronouns(ctx):
pronoun_roles = [role for role in ctx.guild.roles if '/' in role.name]
response = "\n".join(f"{i+1}. {role.name}" for i, role in enumerate(pronoun_roles))
await ctx.send(f"\nplease reply with the number(s) corresponding to your preferred pronouns:\n{response}\n\nif you do not see your preferred pronouns in our listing, please tag the administrator right here.")
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
message = await bot.wait_for('message', check=check, timeout=60.0)
# Extracting numbers using regex
number_strs = re.findall(r'\d+', message.content)
numbers = [int(number) for number in number_strs if 0 < int(number) <= len(pronoun_roles)]
for number in numbers:
role_to_toggle = pronoun_roles[number-1]
if role_to_toggle in ctx.author.roles:
await ctx.author.remove_roles(role_to_toggle)
action = "removed"
else:
await ctx.author.add_roles(role_to_toggle)
action = "added"
await ctx.send(f"the role {role_to_toggle.name} has been {action}.")
except asyncio.TimeoutError:
await ctx.send('you took too long to respond. try sending "``$pronouns``" again.')
return
@bot.command()
async def assign(ctx, *args):
message = ctx.message.content
pronoun_roles = [role for role in ctx.guild.roles if '/' in role.name]
# Extract numbers from the message content
number_strs = re.findall(r'\d+', message)
# Initialize 'numbers' as a set to avoid duplicates
numbers = set(int(number) for number in number_strs if 0 < int(number) <= len(pronoun_roles))
# Process args, adding to the 'numbers' set
for arg in args:
try:
number = int(arg)
if 0 < number <= len(pronoun_roles):
numbers.add(number) # 'add' method for sets ensures no duplicates
except ValueError:
continue
# Now 'numbers' contains unique numbers only
for number in numbers:
role_to_toggle = pronoun_roles[number-1]
if role_to_toggle in ctx.author.roles:
await ctx.author.remove_roles(role_to_toggle)
action = "removed"
else:
await ctx.author.add_roles(role_to_toggle)
action = "added"
await ctx.send(f"the role {role_to_toggle.name} has been {action}.")
# terminal print
@bot.event
async def on_ready():
global guild
print('We have logged in as {0.user}'.format(bot))
@bot.event
async def on_member_join(member):
if member.guild.id == CARELESSLOVE_ID:
channel = bot.get_channel(CL_CHATTER)
if channel:
await channel.send(f'welcome to {member.guild.name}, {member.mention}.\na black wind howls. join our futile prattle as we try to fill the void.\n\nif you wish, please tell us which pronouns we should use for you, which you can begin by typing "``$pronouns``"')
else:
channel = bot.get_channel(BGR_WELCOME)
if channel:
await channel.send(f':bubbles::sparkles:welcome to {member.guild.name}, {member.mention}:sparkles::bubbles:\n\nwe are a social network of trans and/or queer folks; a resource for events, information exchange, sharing crafts, mutual aid, and various means of support\n\n**please carefully read the #rules channel, and follow its instructions**\n\nnext, please feel free to introduce yourself, tell us if you would like a custom color for your display name, and tell us which pronouns we should use for you, which you can begin by typing "``$pronouns``"')
bot.run(config['token'])