From 6e41bd2219444b56cbbe6f1bf9bb9b5721e78461 Mon Sep 17 00:00:00 2001 From: Gnome Date: Tue, 31 Aug 2021 20:48:31 +0100 Subject: [PATCH] Remove intents.default and make intents a required parameter --- discord/client.py | 4 +++- discord/ext/commands/bot.py | 4 ++-- discord/flags.py | 10 ---------- discord/state.py | 9 +++------ examples/background_task.py | 2 +- examples/background_task_asyncio.py | 2 +- examples/basic_bot.py | 6 ++---- examples/basic_voice.py | 7 +++++-- examples/converters.py | 3 +-- examples/custom_context.py | 2 +- examples/deleted.py | 2 +- examples/edits.py | 2 +- examples/guessing_game.py | 2 +- examples/new_member.py | 5 +---- examples/reaction_roles.py | 4 +--- examples/reply.py | 2 +- examples/secret.py | 6 +++++- examples/views/confirm.py | 5 ++++- examples/views/counter.py | 5 ++++- examples/views/dropdown.py | 7 ++++--- examples/views/ephemeral.py | 5 ++++- examples/views/link.py | 7 +++++-- examples/views/persistent.py | 6 +++++- examples/views/tic_tac_toe.py | 5 ++++- 24 files changed, 60 insertions(+), 52 deletions(-) diff --git a/discord/client.py b/discord/client.py index b6198d10..b4f1db17 100644 --- a/discord/client.py +++ b/discord/client.py @@ -142,7 +142,6 @@ class Client: intents: :class:`Intents` The intents that you want to enable for the session. This is a way of disabling and enabling certain gateway events from triggering and being sent. - If not given, defaults to a regularly constructed :class:`Intents` class. .. versionadded:: 1.5 member_cache_flags: :class:`MemberCacheFlags` @@ -203,9 +202,12 @@ class Client: def __init__( self, *, + intents: Intents, loop: Optional[asyncio.AbstractEventLoop] = None, **options: Any, ): + options["intents"] = intents + # self.ws is set in the connect method self.ws: DiscordWebSocket = None # type: ignore self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() if loop is None else loop diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index b4da6100..c089b87d 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -120,8 +120,8 @@ class _DefaultRepr: _default = _DefaultRepr() class BotBase(GroupMixin): - def __init__(self, command_prefix, help_command=_default, description=None, **options): - super().__init__(**options) + def __init__(self, command_prefix, help_command=_default, description=None, *, intents: discord.Intents, **options): + super().__init__(**options, intents=intents) self.command_prefix = command_prefix self.extra_events: Dict[str, List[CoroFunc]] = {} self.__cogs: Dict[str, Cog] = {} diff --git a/discord/flags.py b/discord/flags.py index fb468c50..3c5956a4 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -480,16 +480,6 @@ class Intents(BaseFlags): self.value = self.DEFAULT_VALUE return self - @classmethod - def default(cls: Type[Intents]) -> Intents: - """A factory method that creates a :class:`Intents` with everything enabled - except :attr:`presences` and :attr:`members`. - """ - self = cls.all() - self.presences = False - self.members = False - return self - @flag_value def guilds(self): """:class:`bool`: Whether guild related events are enabled. diff --git a/discord/state.py b/discord/state.py index 2534e7aa..0a9feac1 100644 --- a/discord/state.py +++ b/discord/state.py @@ -152,6 +152,7 @@ class ConnectionState: handlers: Dict[str, Callable], hooks: Dict[str, Callable], http: HTTPClient, + intents: Intents, loop: asyncio.AbstractEventLoop, **options: Any, ) -> None: @@ -194,12 +195,8 @@ class ConnectionState: else: status = str(status) - intents = options.get('intents', None) - if intents is not None: - if not isinstance(intents, Intents): - raise TypeError(f'intents parameter must be Intent not {type(intents)!r}') - else: - intents = Intents.default() + if not isinstance(intents, Intents): + raise TypeError(f'intents parameter must be Intent not {type(intents)!r}') if not intents.guilds: _log.warning('Guilds intent seems to be disabled. This may cause state related issues.') diff --git a/examples/background_task.py b/examples/background_task.py index 77ca885a..bf68d880 100644 --- a/examples/background_task.py +++ b/examples/background_task.py @@ -26,5 +26,5 @@ class MyClient(discord.Client): async def before_my_task(self): await self.wait_until_ready() # wait until the bot logs in -client = MyClient() +client = MyClient(intents=discord.Intents(guilds=True)) client.run('token') diff --git a/examples/background_task_asyncio.py b/examples/background_task_asyncio.py index be445e91..c203afba 100644 --- a/examples/background_task_asyncio.py +++ b/examples/background_task_asyncio.py @@ -22,5 +22,5 @@ class MyClient(discord.Client): await asyncio.sleep(60) # task runs every 60 seconds -client = MyClient() +client = MyClient(intents=discord.Intents(guilds=True)) client.run('token') diff --git a/examples/basic_bot.py b/examples/basic_bot.py index d350401e..805a76d3 100644 --- a/examples/basic_bot.py +++ b/examples/basic_bot.py @@ -9,10 +9,8 @@ module. There are a number of utility commands being showcased here.''' -intents = discord.Intents.default() -intents.members = True - -bot = commands.Bot(command_prefix='?', description=description, intents=intents) +intents = discord.Intents(guilds=True, messages=True, members=True) +bot = commands.Bot(command_prefix='t-', description=description, intents=intents) @bot.event async def on_ready(): diff --git a/examples/basic_voice.py b/examples/basic_voice.py index 9f47c77a..8de120c8 100644 --- a/examples/basic_voice.py +++ b/examples/basic_voice.py @@ -123,8 +123,11 @@ class Music(commands.Cog): elif ctx.voice_client.is_playing(): ctx.voice_client.stop() -bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"), - description='Relatively simple music bot example') +bot = commands.Bot( + command_prefix=commands.when_mentioned_or("!"), + description='Relatively simple music bot example', + intents=discord.Intents(guilds=True, guild_messages=True, voice_states=True) +) @bot.event async def on_ready(): diff --git a/examples/converters.py b/examples/converters.py index 9bd8ae06..54539201 100644 --- a/examples/converters.py +++ b/examples/converters.py @@ -5,9 +5,8 @@ import typing import discord from discord.ext import commands -intents = discord.Intents.default() -intents.members = True +intents = discord.Intents(guilds=True, messages=True, members=True) bot = commands.Bot('!', intents=intents) diff --git a/examples/custom_context.py b/examples/custom_context.py index d3a5b94b..2c5d6630 100644 --- a/examples/custom_context.py +++ b/examples/custom_context.py @@ -29,7 +29,7 @@ class MyBot(commands.Bot): return await super().get_context(message, cls=cls) -bot = MyBot(command_prefix='!') +bot = MyBot(command_prefix='!', intents=discord.Intents(guilds=True, messages=True)) @bot.command() async def guess(ctx, number: int): diff --git a/examples/deleted.py b/examples/deleted.py index 6e0c203d..07863d25 100644 --- a/examples/deleted.py +++ b/examples/deleted.py @@ -17,5 +17,5 @@ class MyClient(discord.Client): msg = f'{message.author} has deleted the message: {message.content}' await message.channel.send(msg) -client = MyClient() +client = MyClient(intents=discord.Intents(guilds=True, messages=True)) client.run('token') diff --git a/examples/edits.py b/examples/edits.py index 36786046..de03a157 100644 --- a/examples/edits.py +++ b/examples/edits.py @@ -16,5 +16,5 @@ class MyClient(discord.Client): msg = f'**{before.author}** edited their message:\n{before.content} -> {after.content}' await before.channel.send(msg) -client = MyClient() +client = MyClient(intents=discord.Intents(guilds=True, messages=True)) client.run('token') diff --git a/examples/guessing_game.py b/examples/guessing_game.py index c96a18e3..f8ad4a1e 100644 --- a/examples/guessing_game.py +++ b/examples/guessing_game.py @@ -30,5 +30,5 @@ class MyClient(discord.Client): else: await message.channel.send(f'Oops. It is actually {answer}.') -client = MyClient() +client = MyClient(intents=discord.Intents(guilds=True, messages=True)) client.run('token') diff --git a/examples/new_member.py b/examples/new_member.py index 7081639b..d35d98f2 100644 --- a/examples/new_member.py +++ b/examples/new_member.py @@ -14,8 +14,5 @@ class MyClient(discord.Client): await guild.system_channel.send(to_send) -intents = discord.Intents.default() -intents.members = True - -client = MyClient(intents=intents) +client = MyClient(intents=discord.Intents(guilds=True, members=True)) client.run('token') diff --git a/examples/reaction_roles.py b/examples/reaction_roles.py index b58e7f74..d213f46b 100644 --- a/examples/reaction_roles.py +++ b/examples/reaction_roles.py @@ -78,8 +78,6 @@ class MyClient(discord.Client): # If we want to do something in case of errors we'd do it here. pass -intents = discord.Intents.default() -intents.members = True - +intents = discord.Intents(guilds=True, members=True, guild_reactions=True) client = MyClient(intents=intents) client.run('token') diff --git a/examples/reply.py b/examples/reply.py index b35ac669..c57c47a5 100644 --- a/examples/reply.py +++ b/examples/reply.py @@ -13,5 +13,5 @@ class MyClient(discord.Client): if message.content.startswith('!hello'): await message.reply('Hello!', mention_author=True) -client = MyClient() +client = MyClient(intents=discord.Intents(guilds=True, messages=True)) client.run('token') diff --git a/examples/secret.py b/examples/secret.py index 9246c68f..2ad2471f 100644 --- a/examples/secret.py +++ b/examples/secret.py @@ -3,7 +3,11 @@ import typing import discord from discord.ext import commands -bot = commands.Bot(command_prefix=commands.when_mentioned, description="Nothing to see here!") +bot = commands.Bot( + command_prefix=commands.when_mentioned, + description="Nothing to see here!", + intents=discord.Intents(guilds=True, messages=True) +) # the `hidden` keyword argument hides it from the help command. @bot.group(hidden=True) diff --git a/examples/views/confirm.py b/examples/views/confirm.py index 6ec81369..a22876b4 100644 --- a/examples/views/confirm.py +++ b/examples/views/confirm.py @@ -5,7 +5,10 @@ import discord class Bot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) async def on_ready(self): print(f'Logged in as {self.user} (ID: {self.user.id})') diff --git a/examples/views/counter.py b/examples/views/counter.py index a1ab756a..799bb7ea 100644 --- a/examples/views/counter.py +++ b/examples/views/counter.py @@ -5,7 +5,10 @@ import discord class CounterBot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) async def on_ready(self): print(f'Logged in as {self.user} (ID: {self.user.id})') diff --git a/examples/views/dropdown.py b/examples/views/dropdown.py index db6d699a..993f4d03 100644 --- a/examples/views/dropdown.py +++ b/examples/views/dropdown.py @@ -1,5 +1,3 @@ -import typing - import discord from discord.ext import commands @@ -39,7 +37,10 @@ class DropdownView(discord.ui.View): class Bot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) async def on_ready(self): print(f'Logged in as {self.user} (ID: {self.user.id})') diff --git a/examples/views/ephemeral.py b/examples/views/ephemeral.py index 770d4b65..85f57074 100644 --- a/examples/views/ephemeral.py +++ b/examples/views/ephemeral.py @@ -4,7 +4,10 @@ import discord class EphemeralCounterBot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) async def on_ready(self): print(f'Logged in as {self.user} (ID: {self.user.id})') diff --git a/examples/views/link.py b/examples/views/link.py index 2516d538..ec000907 100644 --- a/examples/views/link.py +++ b/examples/views/link.py @@ -5,7 +5,10 @@ from urllib.parse import quote_plus class GoogleBot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) async def on_ready(self): print(f'Logged in as {self.user} (ID: {self.user.id})') @@ -36,4 +39,4 @@ async def google(ctx: commands.Context, *, query: str): await ctx.send(f'Google Result for: `{query}`', view=Google(query)) -bot.run('token') +bot.run() diff --git a/examples/views/persistent.py b/examples/views/persistent.py index 71747667..261e0679 100644 --- a/examples/views/persistent.py +++ b/examples/views/persistent.py @@ -29,7 +29,11 @@ class PersistentView(discord.ui.View): class PersistentViewBot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) + self.persistent_views_added = False async def on_ready(self): diff --git a/examples/views/tic_tac_toe.py b/examples/views/tic_tac_toe.py index 81632e26..c3665073 100644 --- a/examples/views/tic_tac_toe.py +++ b/examples/views/tic_tac_toe.py @@ -120,7 +120,10 @@ class TicTacToe(discord.ui.View): class TicTacToeBot(commands.Bot): def __init__(self): - super().__init__(command_prefix=commands.when_mentioned_or('$')) + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents(guilds=True, messages=True) + ) async def on_ready(self): print(f'Logged in as {self.user} (ID: {self.user.id})') -- 2.47.2