mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-04 18:00:08 +00:00
Merge branch 'master' of https://github.com/Skillz4Killz/Discordeno into rename-permission_overwrites
This commit is contained in:
2
egg.yml
2
egg.yml
@@ -2,7 +2,7 @@ name: Discordeno
|
||||
description: >-
|
||||
Discord Deno TypeScript API library wrapper(Officially vetted library by
|
||||
Discord Team) https://discordeno.netlify.app
|
||||
version: 9.0.12
|
||||
version: 9.0.15
|
||||
stable: true
|
||||
entry: mod.ts
|
||||
repository: 'https://github.com/Skillz4Killz/Discordeno'
|
||||
|
||||
@@ -14,16 +14,19 @@ import {
|
||||
MessageContent,
|
||||
} from "../types/channel.ts";
|
||||
import { Errors } from "../types/errors.ts";
|
||||
import { RawOverwrite } from "../types/guild.ts";
|
||||
import { PermissionOverwrite } from "../types/guild.ts";
|
||||
import { MessageCreateOptions } from "../types/message.ts";
|
||||
import { Permissions } from "../types/permission.ts";
|
||||
import { botHasChannelPermissions } from "../utils/permissions.ts";
|
||||
import {
|
||||
botHasChannelPermissions,
|
||||
calculateBits,
|
||||
} from "../utils/permissions.ts";
|
||||
|
||||
/** Checks if a channel overwrite for a user id or a role id has permission in this channel */
|
||||
export function channelOverwriteHasPermission(
|
||||
guildID: string,
|
||||
id: string,
|
||||
overwrites: RawOverwrite[],
|
||||
overwrites: PermissionOverwrite[],
|
||||
permissions: Permissions[],
|
||||
) {
|
||||
const overwrite = overwrites.find((perm) => perm.id === id) ||
|
||||
@@ -31,8 +34,10 @@ export function channelOverwriteHasPermission(
|
||||
|
||||
return permissions.every((perm) => {
|
||||
if (overwrite) {
|
||||
if (BigInt(overwrite.deny) & BigInt(perm)) return false;
|
||||
if (BigInt(overwrite.allow) & BigInt(perm)) return true;
|
||||
const allowBits = calculateBits(overwrite.allow);
|
||||
const denyBits = calculateBits(overwrite.deny);
|
||||
if (BigInt(denyBits) & BigInt(perm)) return false;
|
||||
if (BigInt(allowBits) & BigInt(perm)) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { assert, assertArrayIncludes, assertEquals, delay } from "../deps.ts";
|
||||
import { assert, assertEquals, delay } from "../deps.ts";
|
||||
import {
|
||||
botID,
|
||||
cache,
|
||||
@@ -18,10 +18,13 @@ import {
|
||||
Role,
|
||||
sendMessage,
|
||||
} from "../mod.ts";
|
||||
import { editChannel } from "../src/handlers/channel.ts";
|
||||
import {
|
||||
channelOverwriteHasPermission,
|
||||
editChannel,
|
||||
} from "../src/handlers/channel.ts";
|
||||
import { getChannel } from "../src/handlers/guild.ts";
|
||||
import { Permissions } from "../src/types/permission.ts";
|
||||
|
||||
// TODO: add DISCORD_TOKEN variable to GitHub secrets
|
||||
const token = Deno.env.get("DISCORD_TOKEN");
|
||||
if (!token) throw "Token is not provided";
|
||||
|
||||
@@ -143,11 +146,19 @@ Deno.test({
|
||||
async fn() {
|
||||
const channel = await editChannel(data.channelID, {
|
||||
name: "edited channel",
|
||||
overwrites: [
|
||||
{
|
||||
id: data.roleID,
|
||||
type: OverwriteType.ROLE,
|
||||
allow: ["VIEW_CHANNEL", "SEND_MESSAGES"],
|
||||
deny: ["USE_EXTERNAL_EMOJIS"],
|
||||
},
|
||||
],
|
||||
}) as Channel;
|
||||
const editedChannel = await getChannel(data.channelID);
|
||||
|
||||
assert(channel);
|
||||
|
||||
data.channelID = channel.id;
|
||||
assertEquals(editedChannel.name, "edited channel");
|
||||
},
|
||||
});
|
||||
|
||||
@@ -156,20 +167,24 @@ Deno.test({
|
||||
async fn() {
|
||||
const channel = cache.channels.get(data.channelID);
|
||||
if (!channel) throw "Channel not found";
|
||||
assertArrayIncludes(channel.permissionOverwrites!, [
|
||||
{
|
||||
id: data.roleID,
|
||||
type: OverwriteType.ROLE,
|
||||
// The type for Channel#permission_overwrites is "RawOverwrite[] | undefined"
|
||||
// not "Overwrite[]"; therefore, permission strings cannot be used.
|
||||
// allow: ["VIEW_CHANNEL", "SEND_MESSAGES"],
|
||||
// deny: ["USE_EXTERNAL_EMOJIS"],
|
||||
},
|
||||
]);
|
||||
|
||||
// THIS TEST CASE SHOULD BE REFACTORED AND IMPROVED
|
||||
// CURRENTLY, IT USES Channel#permission_overwrites
|
||||
// but preferably, it should use the channelOverwriteHasPermission()
|
||||
if (!channel.permissionOverwrites) throw "Channel overwrites not found.";
|
||||
|
||||
const hasPerm = channelOverwriteHasPermission(
|
||||
data.guildID,
|
||||
data.roleID,
|
||||
channel.permissionOverwrites,
|
||||
[Permissions.VIEW_CHANNEL, Permissions.SEND_MESSAGES],
|
||||
);
|
||||
const missingPerm = channelOverwriteHasPermission(
|
||||
data.guildID,
|
||||
data.roleID,
|
||||
channel.permissionOverwrites,
|
||||
[Permissions.USE_EXTERNAL_EMOJIS],
|
||||
);
|
||||
|
||||
assertEquals(hasPerm, true);
|
||||
assertEquals(missingPerm, false);
|
||||
},
|
||||
...testOptions,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user