-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot
125 lines (107 loc) · 4.67 KB
/
bot
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
117
118
119
120
121
122
123
124
125
@bot.command()
@commands.has_role('Staff')
async def delete():
'''This command is usable by Staff only. Cleans up the Nominations file.'''
try:
os.remove('nominations.csv')
await bot.say('File has been deleted. New file will be created next nomination.')
except FileNotFoundError:
await bot.say('File was not found.')
@bot.command(pass_context=True)
async def nominate(ctx, user: discord.User, *reason: str):
'''Usable to nominate a User for Triumphant. Format: @user , `Reason`'''
user_id = user.id
# Joins the reasons so they become a single string.
not_split_reason = " ".join(reason)
not_split_reason = not_split_reason.strip(',')
not_split_reason = not_split_reason.strip('"')
# Creates a dictionary for the user to be stored to be put into the CSV File.
name_list = {'name': user_id, 'reason': not_split_reason}
with open('nominations.csv', 'a') as csv_file:
writer = csv.writer(csv_file)
for key, value in name_list.items():
writer.writerow([key, value])
await bot.say('Nominated {} for {}'.format(user, not_split_reason))
@bot.command()
@commands.has_role('Staff')
async def remove(user: discord.User):
"""This command removes a member from the nomination file."""
user_id_removal = user.id
people = []
reasons = []
list_for_csv = {}
with open('nominations.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
name_counter = 1
list_counter = 1
for row in csv_reader:
# Iterates through the CSV file to get names of the users and reasons for nominations.
# Also adds the Triumphant role to the user.
if name_counter == 1:
user_id = str(row[1])
name_counter += 1
people.append(user_id)
elif name_counter == 2:
user_reason = row[1]
reasons.append(user_reason)
name_counter = 1
list_counter += 1
i = 0
for name in people:
if name == user_id_removal:
people.pop(i)
reasons.pop(i)
i += 1
print('Final people {}'.format(people))
print('Final reasons {}'.format(reasons))
with open('nominations.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
j = 0
for name in people:
list_for_csv = {'name': people[j], 'reason': reasons[j]}
for key, value in list_for_csv.items():
writer.writerow([key, value])
j += 1
await bot.say('You have removed all of the nominations for that Character')
@commands.has_role('Staff')
@bot.command(pass_context=True)
async def post(ctx):
# Sets up necessary variables.
names = []
reasons = []
server = bot.get_server('258389892352966674')
triumphant_role = discord.utils.get(server.roles, name='Triumphant')
await bot.send_typing(ctx.message.channel)
# Removes Triumphant Role from previous users.
for member in server.members:
for role in discord.Member.roles:
if role.name == "Triumphant":
await bot.remove_roles(member, triumphant_role)
# Writes the CSV file.
with open('nominations.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
name_counter = 1
list_counter = 0
for row in csv_reader:
# Iterates through the CSV file to get names of the users and reasons for nominations.
# Also adds the Triumphant role to the user.
if name_counter == 1:
user_id = str(row[1])
username = server.get_member(user_id=user_id)
try:
await bot.add_roles(username, triumphant_role)
except HTTPException:
await bot.say('Adding triumphant_role failed. Perhaps you have insufficient roles.')
display_username = username.display_name
names.append(str(display_username))
name_counter += 1
list_counter += 1
elif name_counter == 2:
reasons.append(row[1])
name_counter = 1
# Uses embed to post the names and reasons.
embed = discord.Embed(title="Triumphant!", description='Congratulations to the triumphant this week!',
color=0x00ff00)
embed.add_field(name="Names:", value='\n'.join(names), inline=True)
embed.add_field(name="Reasons:", value='\n'.join(reasons), inline=True)
await bot.say(embed=embed)