Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby authored Aug 20, 2022
2 parents 732a7ae + 04da5e8 commit 80615d1
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/vulnerability_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ body:
attributes:
label: Checklist
description: >
Let's make sure you've properly done due dilligence when reporting this issue!
Let's make sure you've properly done due diligence when reporting this issue!
options:
- label: I have searched the open issues for duplicates.
required: true
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codespell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: pip install codespell
- run: codespell --ignore-words-list="groupt,nd,ot"
- run: pip install codespell==2.1.0
- run: codespell --ignore-words-list="groupt,nd,ot,ro,falsy,BU" --exclude-file=".github/workflows/codespell.yml"
2 changes: 1 addition & 1 deletion discord/ext/bridge/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BridgeContext(ABC):
this class are meant to give parity between the two contexts, while still allowing for all of their functionality.
When this is passed to a command, it will either be passed as :class:`BridgeExtContext`, or
:class:`BridgeApplicationContext`. Since they are two separate classes, it is quite simple to use :func:`isinstance`
:class:`BridgeApplicationContext`. Since they are two separate classes, it's easy to use the :attr:`BridgeContext.is_app` attribute.
to make different functionality for each context. For example, if you want to respond to a command with the command
type that it was invoked with, you can do the following:
Expand Down
9 changes: 6 additions & 3 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ def _from_data(self, data: InteractionPayload):

# TODO: there's a potential data loss here
if self.guild_id:
guild = self.guild or Object(id=self.guild_id)
guild = self.guild or self._state._get_guild(self.guild_id) or Object(id=self.guild_id)
try:
member = data["member"] # type: ignore
except KeyError:
pass
else:
cache_flag = self._state.member_cache_flags.interaction
self.user = guild._get_and_update_member(member, int(member["user"]["id"]), cache_flag)
self._permissions = int(member.get("permissions", 0))
if not isinstance(guild, Object):
cache_flag = self._state.member_cache_flags.interaction
self.user = guild._get_and_update_member(member, int(member["user"]["id"]), cache_flag)
else:
self.user = Member(state=self._state, data=member, guild=guild)
else:
try:
self.user = User(state=self._state, data=data["user"])
Expand Down
2 changes: 2 additions & 0 deletions discord/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ async def fill_bans(self):
if not data:
# no data, terminate
return
if self.limit:
self.limit -= self.retrieve

if len(data) < 1000:
self.limit = 0 # terminate loop
Expand Down
2 changes: 1 addition & 1 deletion discord/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class View:
The list of children attached to this view.
disable_on_timeout: :class:`bool`
Whether to disable the view when the timeout is reached. Defaults to ``False``.
message: Optional[:class:`Message`]
message: Optional[:class:`.Message`]
The message that this view is attached to.
If ``None`` then the view has not been sent with a message.
"""
Expand Down
43 changes: 24 additions & 19 deletions examples/deleted.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# This example requires the `message_content` privileged intent for access to message content.

import discord

intents = discord.Intents.default()
intents.message_content = True # < This may give you `read-only` warning, just ignore it.
# This intent requires "Message Content Intent" to be enabled at https://discord.com/developers


class MyClient(discord.Client):
async def on_ready(self):
print(f"Logged in as {self.user} (ID: {self.user.id})")
print("------")
bot = discord.Bot(intents=intents)

async def on_message(self, message: discord.Message):
if message.content.startswith("!deleteme"):
msg = await message.channel.send("I will delete myself now...")
await msg.delete()

# This also works:
await message.channel.send("Goodbye in 3 seconds...", delete_after=3.0)
@bot.event
async def on_ready():
print('Ready!')

async def on_message_delete(self, message: discord.Message):
msg = f"{message.author} has deleted the message: {message.content}"
await message.channel.send(msg)

@bot.event
async def on_message(message: discord.Message):
if message.content.startswith("!deleteme"):
msg = await message.channel.send("I will delete myself now...")
await msg.delete()

intents = discord.Intents.default()
intents.message_content = True
# This also works:
await message.channel.send("Goodbye in 3 seconds...", delete_after=3.0)


@bot.event
async def on_message_delete(message: discord.Message):
msg = f"{message.author} has deleted the message: {message.content}"
await message.channel.send(msg)


bot.run("TOKEN")

client = MyClient(intents=intents)
client.run("TOKEN")
41 changes: 22 additions & 19 deletions examples/edits.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# This example requires the `message_content` privileged intent for access to message content.

import discord
import asyncio

import discord
intents = discord.Intents.default()
intents.message_content = True # < This may give you `read-only` warning, just ignore it.
# This intent requires "Message Content Intent" to be enabled at https://discord.com/developers


class MyClient(discord.Client):
async def on_ready(self):
print(f"Logged in as {self.user} (ID: {self.user.id})")
print("------")
bot = discord.Bot(intents=intents)

async def on_message(self, message: discord.Message):
if message.content.startswith("!editme"):
msg = await message.channel.send("10")
await asyncio.sleep(3.0)
await msg.edit(content="40")

async def on_message_edit(self, before: discord.Message, after: discord.Message):
msg = f"**{before.author}** edited their message:\n{before.content} -> {after.content}"
await before.channel.send(msg)
@bot.event
async def on_ready():
print('Ready!')


intents = discord.Intents.default()
intents.message_content = True
@bot.event
async def on_message(message: discord.Message):
if message.content.startswith("!editme"):
msg = await message.channel.send("10")
await asyncio.sleep(3.0)
await msg.edit(content="40")


@bot.event
async def on_message_edit(before: discord.Message, after: discord.Message):
msg = f"**{before.author}** edited their message:\n{before.content} -> {after.content}"
await before.channel.send(msg)


client = MyClient(intents=intents)
client.run("TOKEN")
bot.run("TOKEN")
175 changes: 91 additions & 84 deletions examples/reaction_roles.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,94 @@
# This example requires the 'members' privileged intent for access to .get_member.

import discord


class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.role_message_id = 0 # ID of the message that can be reacted to for adding/removing a role.
self.emoji_to_role = {
discord.PartialEmoji(name="🔴"): 0, # ID of the role associated with unicode emoji '🔴'.
discord.PartialEmoji(name="🟡"): 0, # ID of the role associated with unicode emoji '🟡'.
discord.PartialEmoji(name="green", id=0): 0, # ID of the role associated with a partial emoji's ID.
}

async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
"""Gives a role based on a reaction emoji."""
# Make sure that the message the user is reacting to is the one we care about.
if payload.message_id != self.role_message_id:
return

guild = self.get_guild(payload.guild_id)
if guild is None:
# Make sure we're still in the guild, and it's cached.
return

try:
role_id = self.emoji_to_role[payload.emoji]
except KeyError:
# If the emoji isn't the one we care about then exit as well.
return

role = guild.get_role(role_id)
if role is None:
# Make sure the role still exists and is valid.
return

try:
# Finally, add the role.
await payload.member.add_roles(role)
except discord.HTTPException:
# If we want to do something in case of errors we'd do it here.
pass

async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
"""Removes a role based on a reaction emoji."""
# Make sure that the message the user is reacting to is the one we care about.
if payload.message_id != self.role_message_id:
return

guild = self.get_guild(payload.guild_id)
if guild is None:
# Make sure we're still in the guild, and it's cached.
return

try:
role_id = self.emoji_to_role[payload.emoji]
except KeyError:
# If the emoji isn't the one we care about then exit as well.
return

role = guild.get_role(role_id)
if role is None:
# Make sure the role still exists and is valid.
return

# The payload for `on_raw_reaction_remove` does not provide `.member`
# so we must get the member ourselves from the payload's `.user_id`.
member = guild.get_member(payload.user_id)
if member is None:
# Make sure the member still exists and is valid.
return

try:
# Finally, remove the role.
await member.remove_roles(role)
except discord.HTTPException:
# If we want to do something in case of errors we'd do it here.
pass

import asyncio

intents = discord.Intents.default()
intents.members = True

client = MyClient(intents=intents)
client.run("TOKEN")
intents.members = True # < This may give you `read-only` warning, just ignore it.
# This intent requires "Server Members Intent" to be enabled at https://discord.com/developers


bot = discord.Bot(intents=intents)

role_message_id = 0 # ID of the message that can be reacted to for adding/removing a role.

emoji_to_role = {
discord.PartialEmoji(name="🔴"): 0, # ID of the role associated with unicode emoji '🔴'.
discord.PartialEmoji(name="🟡"): 0, # ID of the role associated with unicode emoji '🟡'.
discord.PartialEmoji(name="green", id=0): 0, # ID of the role associated with a partial emoji's ID.
}


@bot.event
async def on_ready():
print('Ready!')


@bot.event
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
"""Gives a role based on a reaction emoji."""
# Make sure that the message the user is reacting to is the one we care about.
if payload.message_id != role_message_id:
return

guild = bot.get_guild(payload.guild_id)
if guild is None:
# Make sure we're still in the guild, and it's cached.
return

try:
role_id = emoji_to_role[payload.emoji]
except KeyError:
# If the emoji isn't the one we care about then exit as well.
return

role = guild.get_role(role_id)
if role is None:
# Make sure the role still exists and is valid.
return

try:
# Finally, add the role.
await payload.member.add_roles(role)
except discord.HTTPException:
# If we want to do something in case of errors we'd do it here.
pass


@bot.event
async def on_raw_reaction_remove(payload: discord.RawReactionActionEvent):
"""Removes a role based on a reaction emoji."""
# Make sure that the message the user is reacting to is the one we care about.
if payload.message_id != role_message_id:
return

guild = bot.get_guild(payload.guild_id)
if guild is None:
# Make sure we're still in the guild, and it's cached.
return

try:
role_id = emoji_to_role[payload.emoji]
except KeyError:
# If the emoji isn't the one we care about then exit as well.
return

role = guild.get_role(role_id)
if role is None:
# Make sure the role still exists and is valid.
return

# The payload for `on_raw_reaction_remove` does not provide `.member`
# so we must get the member ourselves from the payload's `.user_id`.
member = guild.get_member(payload.user_id)
if member is None:
# Make sure the member still exists and is valid.
return

try:
# Finally, remove the role.
await member.remove_roles(role)
except discord.HTTPException:
# If we want to do something in case of errors we'd do it here.
pass


bot.run("TOKEN")
34 changes: 18 additions & 16 deletions examples/reply.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# This example requires the `message_content` privileged intent for access to message content.

import discord

intents = discord.Intents.default()
intents.message_content = True # < This may give you `read-only` warning, just ignore it.
# This intent requires "Message Content Intent" to be enabled at https://discord.com/developers

class MyClient(discord.Client):
async def on_ready(self):
print(f"Logged in as {self.user} (ID: {self.user.id})")
print("------")

async def on_message(self, message: discord.Message):
# Make sure we won't be replying to ourselves.
if message.author.id == self.user.id:
return
bot = discord.Bot(intents=intents)

if message.content.startswith("!hello"):
await message.reply("Hello!", mention_author=True)

@bot.event
async def on_ready():
print('Ready!')


@bot.event
async def on_message(message: discord.Message):
# Make sure we won't be replying to ourselves.
if message.author.id == bot.user.id:
return

if message.content.startswith("!hello"):
await message.reply("Hello!", mention_author=True)

intents = discord.Intents.default()
intents.message_content = True

client = MyClient(intents=intents)
client.run("TOKEN")
bot.run("TOKEN")
Loading

0 comments on commit 80615d1

Please sign in to comment.