Skip to content

Commit

Permalink
feat: update OAuth2 authorization options and add method warnings for…
Browse files Browse the repository at this point in the history
… deprecated functionality
  • Loading branch information
aiko-chan-ai committed Nov 27, 2024
1 parent 30a7147 commit a12b84d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion examples/AddBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ const client = new Discord.Client({

client.on('ready', async () => {
console.log('Ready!', client.user.tag);
// Note
// You need to include `guild_id` and `permissions` to invite the bot.
// These two fields can appear either in the URL or in the options.
await client.authorizeURL(
`https://discord.com/api/oauth2/authorize?client_id=289066747443675143&permissions=414501424448&scope=bot%20applications.commands`,
{
guild_id: 'guild id',
permissions: '8', // Admin
permissions: '414501424448',
authorize: true,
},
);
Expand Down
20 changes: 18 additions & 2 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ class Client extends BaseClient {
* await client.acceptInvite('https://discord.gg/genshinimpact', { bypassOnboarding: true, bypassVerify: true })
*/
async acceptInvite(invite, options = { bypassOnboarding: true, bypassVerify: true }) {
throw new Error('METHOD_WARNING');
const code = DataResolver.resolveInviteCode(invite);
if (!code) throw new Error('INVITE_RESOLVE_CODE');
const i = await this.fetchInvite(code);
Expand Down Expand Up @@ -709,7 +710,8 @@ class Client extends BaseClient {
authorize: true
})
*/
authorizeURL(url, options = { authorize: true, permissions: '0' }) {
authorizeURL(url, options = { authorize: true }) {
throw new Error('METHOD_WARNING');
const pathnameAPI = /\/api\/(v\d{1,2}\/)?oauth2\/authorize/;
const pathnameURL = /\/oauth2\/authorize/;
const url_ = new URL(url);
Expand All @@ -720,8 +722,19 @@ class Client extends BaseClient {
throw new Error('INVALID_URL', url);
}
const searchParams = Object.fromEntries(url_.searchParams);
options.permissions = `${Permissions.resolve(searchParams.permissions || options.permissions) || 0}`;
options.permissions ??= `${Permissions.resolve(searchParams.permissions || 0)}`;
options.integration_type ??= searchParams.integration_type || 0;
options.location_context = {
guild_id: '10000',
channel_id: '10000',
channel_type: 10000,
};
options.guild_id ??= searchParams.guild_id;
options.authorize ??= true;
delete searchParams.permissions;
delete searchParams.integration_type;
delete searchParams.guild_id;
if (!options.permissions || !options.guild_id) throw new Error('INVALID_OAUTH_OPTIONS');
return this.api.oauth2.authorize.post({
query: searchParams,
data: options,
Expand Down Expand Up @@ -790,6 +803,9 @@ class Client extends BaseClient {
* @private
*/
_validateOptions(options = this.options) {
options.captchaSolver = () => {
throw new Error('METHOD_WARNING');
};
if (typeof options.makeCache !== 'function') {
throw new TypeError('CLIENT_INVALID_OPTION', 'makeCache', 'a function');
}
Expand Down
3 changes: 3 additions & 0 deletions src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ const Messages = {
STREAM_CANNOT_JOIN: 'Cannot join a stream to itself',
VOICE_USER_NOT_STREAMING: 'User is not streaming',
POLL_ALREADY_EXPIRED: 'This poll has already expired.',
INVALID_OAUTH_OPTIONS: 'Invalid options for authenticating with OAuth2.',
METHOD_WARNING:
'This method is flagged as it may lead to a temporary or permanent account ban. Do not use until further notice.',
};

for (const [name, message] of Object.entries(Messages)) register(name, message);

0 comments on commit a12b84d

Please sign in to comment.