Skip to content

Commit e7c6d72

Browse files
authored
Merge pull request #27 from matthewflegg/dev
Fixed reaction role errors and ~poll issues
2 parents f2873a8 + d2d5681 commit e7c6d72

File tree

7 files changed

+83
-39
lines changed

7 files changed

+83
-39
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,19 @@ Deletes all reaction role messages for a particular role.<br>
164164
165165
Randomly chooses an option from a list. Use quote marks "" around the options if they are longer than one word.
166166

167-
⚠️ The slash command version does not support choices with multiple words.
167+
⚠️ `/choose` does not currently support choices with multiple words.
168168

169169
> **~meme** or **/meme**
170170
171171
Sends a random meme from Reddit.
172172

173-
> **~poll `yes/no question`** or **/poll `yes/no question`**
173+
> **~poll `yes/no question` | ~poll `question` `option a`, `option b`, `option c`** or **/poll `yes/no question`**
174174
175-
Creates a poll that users can react with yes or no to.
175+
Creates a poll that users can react with yes or no to.<br>
176+
177+
📢 Put quote marks "" around **all** of the arguments if using the prefix command version.<br>
178+
179+
⚠️ `/poll` does not yet support multiple options.
176180

177181
> **~twitch `streamer name`** or **/twitch `streamer name`**
178182

cogs/misc/misc_cog.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import datetime
66
import random
77
import humanize
8-
98
import discord
9+
10+
from typing import Optional
1011
from discord.ext import commands
1112
from utils.models import TwitchBroadcast
1213

@@ -128,33 +129,63 @@ async def meme(self, ctx: commands.Context):
128129
await ctx.reply(embed=meme)
129130

130131
@commands.command()
131-
async def poll(self, ctx: commands.Context, *poll: str):
132+
async def poll(self, ctx: commands.Context, poll: str, *options: str):
132133
"""
133134
🎲 Creates a simple yes or no poll.
134135
135136
❓ This command is also available as a slash command.
136137
138+
You can create a simple yes or no poll by simply using `~poll`. You can,
139+
however, add up to six options after the question.
140+
137141
Usage:
138142
```
139-
~poll <question>
143+
~poll <question> [...options]
140144
```
141145
Or:
142146
```
143-
/poll
147+
/poll <question>
144148
```
145149
"""
146150
if not poll:
147151
return await ctx.send(f':x: {ctx.author.mention}: You need to specify a question.')
148152

149153
embed = discord.Embed(
150-
title=f"Poll by **{ctx.author.name}**:",
151-
description=" ".join(poll)
154+
title=f"📢 Poll by **{ctx.author.name}**:",
155+
description=f"```❓ {poll}```\n"
152156
)
153157

154-
message = await ctx.send(embed=embed)
158+
# If the user doesn't specify any options, just make a yes/no poll.
159+
if not options:
160+
embed.set_footer(text='Vote ✔️ Yes or ❌ No.')
161+
message = await ctx.send(embed=embed)
162+
163+
await message.add_reaction("✔️")
164+
return await message.add_reaction("❌")
155165

156-
await message.add_reaction("✔️")
157-
await message.add_reaction("❌")
166+
if len(options) < 2:
167+
return await ctx.reply(f"❌ You need to add more than one option.")
168+
169+
if len(options) > 6:
170+
return await ctx.reply(f"❌ You can't add more than 6 options.")
171+
172+
key = {
173+
"A": "🔴",
174+
"B": "🟠",
175+
"C": "🟡",
176+
"D": "🟢",
177+
"E": "🔵",
178+
"F": "🟣"
179+
}
180+
181+
# Add fields
182+
for (letter, emoji), option in zip(key.items(), options):
183+
embed.add_field(name=f'{emoji} Option {letter}:', value=option, inline=False)
184+
185+
# Send message and add reactions
186+
message = await ctx.send(embed=embed)
187+
for (letter, emoji), option in zip(key.items(), options):
188+
await message.add_reaction(emoji)
158189

159190

160191
async def setup(client: commands.Bot):

cogs/role/role_cog.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ async def reactrole(
2828
2929
Usage:
3030
```
31-
~reactrole | ~rrr <@role>
31+
~reactrole | ~crr <emoji> <@role> <message>
3232
```
3333
"""
3434
embed = discord.Embed(description=message)
3535
msg = await ctx.channel.send(embed=embed)
3636

3737
await msg.add_reaction(emoji)
38-
data = self.client.cache.reactionrole
38+
data = self.client.cache.reactionroles
3939

4040
react_role = { # Create a dictionary to store in the JSON file
4141
"guild_id": ctx.guild.id,
@@ -53,22 +53,23 @@ async def reactrole(
5353
async def removereactrole(self, ctx: commands.Context, role: discord.Role):
5454
"""
5555
🏷️ Removes a reaction role message.
56+
5657
Usage:
5758
```
5859
~removereactrole | ~rrr <@role>
5960
```
6061
"""
6162

62-
data = self.client.cache.reactionrole
63+
data = self.client.cache.reactionroles
6364
instances = [item for item in data if item["role_id"] == role.id]
6465
if len(instances) == 0:
6566
raise commands.RoleNotFound(role.__str__())
6667
for instance in instances:
6768
msg = ctx.channel.get_partial_message(instance["msg_id"])
6869
await msg.delete()
6970
data.remove(instance)
70-
self.update_json(FILEPATH, data)
71-
embed = discord.Embed(title=f"🔧 Removed the '{role.name}' reaction role.")
71+
self.client.update_json(FILEPATH, data)
72+
embed = discord.Embed(title="👍🏻 Done.", description=f"🔧 Removed '{role.name}'.")
7273
await ctx.send(embed=embed)
7374

7475
@reactrole.error
@@ -86,36 +87,37 @@ async def reactrole_error(self, ctx: commands.Context, error):
8687
message += "Sorry, that emoji was not found. Please try again."
8788
case commands.UserInputError:
8889
message += (
89-
"Invalid input, please try again.\n"
90-
+ f"Use **{ctx.prefix}reactrole `emoji` `@role` `message`**."
90+
"Invalid input, please try again. "
91+
+ "Use `~help crr` for more information."
9192
)
9293
case commands.MissingRequiredArgument:
9394
message += (
94-
"Please enter all the required arguments.\n"
95-
+ f"Use **{ctx.prefix}reactrole `emoji` `@role` `message`**."
95+
"Please enter all the required arguments. "
96+
+ "Use `~help crr` for more information."
9697
)
9798
case discord.HTTPException: # An invalid emoji raises a HTTP exception
9899
if (
99100
"Unknown Emoji" in error.__str__()
100101
): # Prevents this handler from catching unrelated errors
101102
await ctx.channel.purge(limit=1)
102-
message += "Sorry, that emoji is invalid. Please use a valid emoji."
103+
message += "Sorry, that emoji is invalid."
103104
case discord.Forbidden:
104105
message += (
105-
"BB.Bot is forbidden from assigning/removing this role.\n"
106-
+ f"Try moving this role above the reaction role."
106+
"BB.Bot is forbidden from assigning/removing this role. "
107+
+ "Try moving this role above the reaction role."
107108
)
108109
case _:
110+
print(error.args, error.__traceback__)
109111
message += (
110-
"An unknown error occurred while creating your reaction role.\n"
111-
+ f"Please try again later."
112+
"An unknown error occurred while creating your reaction role. "
113+
+ "Please try again later."
112114
)
113115
await ctx.send(message)
114116

115117
@removereactrole.error
116118
async def removeractrole_error(self, ctx: commands.Context, error):
117119
"""
118-
Error handler for the removeractrole command.
120+
Error handler for the removereactrole command.
119121
"""
120122
error = getattr(error, "original", error)
121123
message = f":x: {ctx.author.mention}: "
@@ -130,20 +132,21 @@ async def removeractrole_error(self, ctx: commands.Context, error):
130132

131133
case commands.UserInputError:
132134
message += (
133-
"Invalid input, please try again.\n"
134-
+ f"Use **{ctx.prefix}reactrole `emoji` `@role` `message`**."
135+
"Invalid input, please try again. "
136+
+ "Use `~help crr` for more information."
135137
)
136138

137139
case commands.MissingRequiredArgument:
138140
message += (
139-
"Please enter all the required arguments.\n"
140-
+ f"Use **{ctx.prefix}removereactrole `@role`**."
141+
"Please enter all the required arguments. "
142+
+ "Use `~help crr` for more information."
141143
)
142144

143145
case _:
146+
print(error.args, error.__traceback__)
144147
message += (
145-
"An unknown error occurred while creating your reaction role.\n"
146-
+ f"Please try again later."
148+
"An unknown error occurred while creating your reaction role. "
149+
+ "Please try again later."
147150
)
148151

149152
await ctx.send(message)

handlers/event_handler.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,19 @@ async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User):
9797
"""
9898
Prevents users from voting more than once on a poll.
9999
"""
100-
user = reaction.message.author
101-
cached = discord.utils.get(self.client.cached_messages, id=reaction.message.id) # why
100+
cached = discord.utils.get(self.client.cached_messages, id=reaction.message.id)
102101

103102
if user.id == self.client.user.id:
104103
return
105104

106105
for react in cached.reactions:
107-
users = await react.users().flatten()
106+
users = [user async for user in react.users()]
108107

109-
if any({user not in users, user.bot, str(react) == str(reaction.emoji)}):
108+
if any({
109+
user not in users,
110+
user.bot,
111+
str(react) == str(reaction.emoji)
112+
}):
110113
continue
111114

112115
await cached.remove_reaction(react.emoji, user)

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def get_prefix(bot: commands.Bot, message: discord.Message):
2020
"""
2121
Returns the client's command prefix.
2222
"""
23-
return '~' if bot.user.name == 'BB.Bot' else '?' # Set the prefix to '?' if the bot is the development version
23+
return '?' if bot.user.name == 'BB.Bot | Dev' else '~' # Set the prefix to '?' if the bot is the development version
2424

2525

2626
class BeepBoop(commands.Bot):
@@ -146,7 +146,7 @@ async def load_handlers(self):
146146
# @client.tree.command(description="People really like this command!")
147147
# async def nice(interaction: discord.Interaction):
148148
# await interaction.response.send_message("Haha, cool indeed!")
149-
149+
i =9
150150
async def main():
151151
"""
152152
Main entry point of the application.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ yarl==1.7.2
2626
youtube-dl==2021.12.17
2727
pillow==9.0.1
2828
humanize
29+
jishaku @ git+https://github.com/Gorialis/jishaku

utils/buttons.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ async def no(self, interaction: discord.Interaction, _: discord.Button):
6262
await interaction.response.send_message('👍🏻 Aborting command!')
6363
await self.disable_all_buttons(interaction)
6464

65+
6566
class BlacklistClearButton(discord.ui.View):
6667
def __init__(self, ctx: commands.Context, *, data: dict, timeout: Optional[float] = 180):
6768
super().__init__(timeout=timeout)
@@ -140,6 +141,7 @@ async def on_submit(self, interaction: discord.Interaction) -> None:
140141
await interaction.message.edit(view=self.view)
141142
return await super().on_submit(interaction=interaction)
142143

144+
143145
class BlacklistDropdown(discord.ui.Select):
144146
def __init__(self, ctx: commands.Context):
145147
self.ctx = ctx

0 commit comments

Comments
 (0)