How to overwrite channel permissions with bitfield - discord

I tried to get my bot to save channel permissions but it only saves its bitfield. Can I somehow overwrite channel permissions for a specific role only using the bitfield?
I tried doing something like this:
channel.overwritePermissions(role, {
permissions: perms
});
and then tried changing it to:
channel.overwritePermissions(role, {
bitfield: perms
});
It just puts / for every permission.

According to the docs of the stable branch, the use of GuildChannel.overwritePermissions() is different from yours:
GuildChannel.overwritePermissions(your_role, {
VIEW_CHANNEL: false,
SEND_MESSAGES: null,
PERMISSIONS_WRITTEN_IN_THIS_FORMAT: true
});
In order to convert the bitfield to a permission name, you can use Permissions.FLAGS, an object that contains all the bitfield values for every permission. Here's the current one:
{ CREATE_INSTANT_INVITE: 1,
KICK_MEMBERS: 2,
BAN_MEMBERS: 4,
ADMINISTRATOR: 8,
MANAGE_CHANNELS: 16,
MANAGE_GUILD: 32,
ADD_REACTIONS: 64,
VIEW_AUDIT_LOG: 128,
PRIORITY_SPEAKER: 256,
VIEW_CHANNEL: 1024,
READ_MESSAGES: 1024,
SEND_MESSAGES: 2048,
SEND_TTS_MESSAGES: 4096,
MANAGE_MESSAGES: 8192,
EMBED_LINKS: 16384,
ATTACH_FILES: 32768,
READ_MESSAGE_HISTORY: 65536,
MENTION_EVERYONE: 131072,
EXTERNAL_EMOJIS: 262144,
USE_EXTERNAL_EMOJIS: 262144,
CONNECT: 1048576,
SPEAK: 2097152,
MUTE_MEMBERS: 4194304,
DEAFEN_MEMBERS: 8388608,
MOVE_MEMBERS: 16777216,
USE_VAD: 33554432,
CHANGE_NICKNAME: 67108864,
MANAGE_NICKNAMES: 134217728,
MANAGE_ROLES: 268435456,
MANAGE_ROLES_OR_PERMISSIONS: 268435456,
MANAGE_WEBHOOKS: 536870912,
MANAGE_EMOJIS: 1073741824 }
To get the name of the permission, you can simply work backwards:
function getPermName(bitfield = 0) {
for (let key in Discord.Permissions.FLAGS)
if (Discord.Permissions.FLAGS[key] == bitfield) return key;
return null;
}
Once you got the name, you can use it as shown above.

To find the name of the permission returned by a simple bitfield.
const { Permissions } = require('discord.js');
const simpleBitfield = 2048n;
Object.entries(Permissions.FLAGS).find(p => p[1] == simpleBitfield);
// outpout: [ 'SEND_MESSAGES', 2048n ]

Related

Discord.py, member.status showing allways offline [duplicate]

#bot.tree.command(name="user", description="Shows some informations about the mentioned user.")
async def user(interaction: discord.Interaction, member:discord.Member=None):
if member == None:
member = interaction.user
roles = [role for role in member.roles if role.name != "#everyone"]
embed = discord.Embed(title=f"Details about the user, {member.name}",color=0xdaddd8, timestamp = datetime.datetime.utcnow())
embed.set_thumbnail(url=member.avatar) # Avatar Thumbnail
embed.add_field(name="👤 Name", value = f"{member.name}#{member.discriminator}") # Embeds
embed.add_field(name="🏷️ Nickname", value = member.display_name)
embed.add_field(name="🆔 User ID", value = member.id)
embed.add_field(name="📆 Created at", value = member.created_at.strftime("%D \n%I:%M %p"))
embed.add_field(name="👋🏻 Joined at", value = member.joined_at.strftime("%D \n%I:%M %p"))
embed.add_field(name="🟢 Status", value = member.status) #this line is'nt working as it should
embed.add_field(name="❤️‍🔥 Top role", value = member.top_role.mention)
bot_status = "Yes, it is" if member.bot else "No, They'snt"
embed.add_field(name="🤖 Bot?", value = bot_status)
embed.set_footer(text = interaction.user.name,icon_url = interaction.user.avatar)
await interaction.response.send_message(embed=embed)
I made a User information command and this command shows every person offline even itself how can i fix it?
This is likely due to lack of intents.
Add in your code, under the intents you define:
intents.members = True
intents.presences = True
Use member.raw_status instead of member.status. It will return a string value such as 'online', 'offline' or 'idle'.

Why am I getting a write after write hazard in vulkan validation layers?

I am trying to get proper synchronization working in trying to get a compute shader writing to an image. However, when I enable validation layers I get the following errors:
[ SYNC-HAZARD-WRITE-AFTER-WRITE ] Object 0: handle = 0x4b7df1000000002f, type = VK_OBJECT_TYPE_IMAGE_VIEW; | MessageID = 0x5c0ec5d6 | vkCmdDispatch: Hazard WRITE_AFTER_WRITE for VkImageView 0x4b7df1000000002f[], in VkCommandBuffer 0x558d7b2aec00[], and VkPipeline 0x95a125000000001a[], VkDescriptorSet 0x944a2c0000000039[], type: VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, imageLayout: VK_IMAGE_LAYOUT_GENERAL, binding #0, index 0. Access info (usage: SYNC_COMPUTE_SHADER_SHADER_STORAGE_WRITE, prior_usage: SYNC_IMAGE_LAYOUT_TRANSITION, write_barriers: 0, command: vkCmdPipelineBarrier, seq_no: 1, reset_no: 1).
[ SYNC-HAZARD-WRITE-AFTER-WRITE ] Object 0: handle = 0xb12fb2000000002c, type = VK_OBJECT_TYPE_IMAGE; | MessageID = 0x5c0ec5d6 | vkCmdPipelineBarrier: Hazard WRITE_AFTER_WRITE for image barrier 0 VkImage 0xb12fb2000000002c[]. Access info (usage: SYNC_IMAGE_LAYOUT_TRANSITION, prior_usage: SYNC_COMPUTE_SHADER_SHADER_STORAGE_WRITE, write_barriers: 0, command: vkCmdDispatch, seq_no: 2, reset_no: 1).
The relevant code is as follows:
vkBeginCommandBuffer(cmdBuffer, &cmdBufBeginInfo);
VkImageMemoryBarrier toGeneralBarrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.pNext = NULL,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = vr.swapImages[imageIndex],
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0,
NULL,
0,
NULL,
1,
&toGeneralBarrier);
// vkCmdBindPipeline, vkCmdBindDescriptorSets is left out, not interesting
vkCmdDispatch(cmdBuffer, dispatchX, dispatchY, 1); // hazard here
VkImageMemoryBarrier toPresentBarrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.pNext = NULL,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = 0,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = vr.swapImages[imageIndex],
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
vkCmdPipelineBarrier( // hazard here?
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0,
0,
NULL,
0,
NULL,
1,
&toPresentBarrier);
// vkEndCommandBuffer, vkQueueSubmit, vkQueuePresentKHR, ... left out
I have tried searching for a mistake in my code that code have caused these errors but I could not find one. Is there something wrong with my pipeline barriers and how do I fix this?

Discord.js Add / Remove role in 1 command

V12 Code
I want to make this command can be used by anyone having a specific role or manage channel perm but it not works, it only allows people having manage channel perm not the people having specific role.
Problem Code
if (!message.member.roles.cache.has('845453361008476190') || !message.member.hasPermission('MANAGE_CHANNELS')) return message.channel.send("sorry, you do not have permission to use command.")
Complete Code
const prefix = process.env.PREFIX;
module.exports = {
name: 'vip',
category: 'moderation',
aliases: ['v'],
description : 'Used give VIP to a User ',
usage: `${prefix}vip <#user>`,
run : async(client, message, args) => {
if (!message.member.roles.cache.has('845453361008476190') || !message.member.hasPermission('MANAGE_CHANNELS')) return message.channel.send("sorry, you do not have permission to use command.")
const guild = client.guilds.cache.get("842019142118014996");
const role = guild.roles.cache.get("845453369564856361");
const aUser = message.mentions.users.first();
if (!aUser) return message.channel.send("Can't find user!");
const member = await guild.members.fetch(aUser.id);
if (member.roles.cache.get(role.id)) {
return (
member.roles.remove(role),
message.channel.send(`Removed VIP role from ${aUser.tag}`)
);
} else {
await member.roles.add(role),
message.channel.send(`${aUser.tag} Sucessfully got VIP role.`);
}
}
};
This checks if the user doesn't have the required role, or doesn't have the required perms. Change the OR operator (||) to an AND operator (&&) so that if checks if the user doesn't have the required role, and doesn't have the required perms, return early
if (!message.member.roles.cache.has('845453361008476190') && !message.member.hasPermission('MANAGE_CHANNELS'))

Error: The argument type 'List<int>' can't be assigned to the parameter type 'Uint8List'

Below is my original codes run well without problem under flutter version 1.22.6, however when I upgraded to flutter version 2.2.1 error of "The argument type 'List' can't be assigned to the parameter type 'Uint8List'." flagged with red error:
final paint = await PaintingBinding.instance!
.instantiateImageCodec(asset != null ? img.encodePng(asset) : buffer);
Anyone could help will be much appreaciated.
This my code:
Future<BeautifulPopup> recolor(Color color) async {
primaryColor = color;
final illustrationData = await rootBundle.load(instance.illustrationKey);
final buffer = illustrationData.buffer.asUint8List();
img.Image asset;
asset = img.readPng(buffer)!;
img.adjustColor(
asset,
saturation: 0,
// hue: 0,
);
img.colorOffset(
asset,
red: primaryColor.red,
// I don't know why the effect is nicer with the number ╮(╯▽╰)╭
green: primaryColor.green ~/ 3,
blue: primaryColor.blue ~/ 2,
alpha: 0,
);
final paint = await PaintingBinding.instance!
.instantiateImageCodec(asset != null ? img.encodePng(asset) : buffer);
final nextFrame = await paint.getNextFrame();
_illustration = nextFrame.image;
return this;
}
...
This is because they are different types. But here is an easy way to handle the conversion:
Uint8List uint8List;
List<int> list;
// convert List<int> to Uint8List.
uint8List = Uint8List.fromList(list);
// convert Uint8List to List<int>
list = List<int>.from(uint8List);
Cheers!
David

Configure multiple roles with same permissions

I've got a quite large website with over 20 roles and permissions. However, it are always the same permissions, but depending on who created the content, the permissions differ...
So what I do now is this:
// Make the new role
$role = new stdClass;
$role->name = 'Redacteur 1';
$role->weight = 3;
user_role_save($role);
// Permissions to assign to the role.
// Note these are defined in hook_permission()
$perms = array(
'access content','access content overview'
);
// Grant the permissions. This function takes care of all necessary cache resets
user_role_grant_permissions($role->rid, $perms);
// Make the new role
$role = new stdClass;
$role->name = 'Redacteur 2';
$role->weight = 3;
user_role_save($role);
// Permissions to assign to the role.
// Note these are defined in hook_permission()
$perms = array(
'access content','access content overview'
);
// Grant the permissions. This function takes care of all necessary cache resets
user_role_grant_permissions($role->rid, $perms);
Isn't there a way to do this with some kind of array so I don't end up with a 1000 line of code. When you want to change something in the permissions, you have to revise all the roles... This must be easier to do. Any advice?
You could change the fields in the database. I wish I could help you more but I have only done minor adjustments.
I wrote some default function to acomplish this goal:
function _load_permission_settings($role_index = null) {
// Blocks
$perms['administer blocks'] = array(0, 0, 0);
// Comments
$perms['administer comments'] = array(0, 1, 0);
$perms['access comments'] = array(1, 1, 1);
$perms['post comments'] = array(1, 1, 1);
$perms['skip comment approval'] = array(1, 1, 1);
$perms['edit own comments'] = array(1, 1, 1);
...
function _create_users() {
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
$roles = user_roles(true);
$users[] = array('an', array(3));
$users[] = array('ben', array(4, 6, 8));
...
function _set_roles_and_permissions() {
// Enable default permissions for system roles.
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content'));
user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, _load_permission_settings(0));
// Permissions to assign to the roles. (all Thema-related roles share the same permissions)
$perms_eind = _load_permission_settings(1);
$perms_red = _load_permission_settings(2);
$user_roles = _load_thema_user_role_names('');
foreach ($user_roles as $name) {
// Role1
$role = new stdClass;
$role->name = 'Role 1 - ' . $name;
user_role_save($role);
user_role_grant_permissions($role->rid, $perms_eind);
// Role2
$role = new stdClass;
$role->name = 'Role 2 - ' . $name;
user_role_save($role);
user_role_grant_permissions($role->rid, $perms_red);
}
}

Resources