问题
So I am using discord.py to make a discord Client. I am using on_voice_state_update to see if the VoiceState of a member changes.
If a member is inside a specific VoiceChannel, I want the client to create a new voice channel, using the member's username as the name of the channel, and move the member inside that new voice channel. Here is my code:
import discord, asyncio
app = discord.Client()
@app.event
async def on_voice_state_update(user_name, user_id, after):
name2 = str(user_name)
ch = app.get_channel(660213767820410918)
guild = app.get_guild(660213767820410893)
member = str(user_id)
if after.channel == ch:
await guild.create_voice_channel(name=(name2+'`s Room'), category=guild.get_channel(660213767820410908) ,user_limit=99 ,overwrites=(user_name ,{'manage_channels': True}))
await guild.member.move_to(channel, reason=None)
This doesn't work. Could anyone please help me?
回答1:
There were a couple of errors.
First of all, the event on_voice_state_update accepts three parameters, which are member, before and after instead of user_name
, user_id
and after
.
When you used the guild.create_voice_channel function, you did not pass a proper dictionary and you forgot to assign channel
to the newly created channel (that is why channel
was undefined).
Finally, you should have used member.move_to instead of guild.member.move_to
as guild.member
is undefined.
import discord, asyncio
app = discord.Client()
@app.event
async def on_voice_state_update(member, before, after):
username = str(member)
guild = app.get_guild(660213767820410893)
ch = guild.get_channel(660213767820410918)
if after.channel == ch:
channel = await guild.create_voice_channel(
name = (username+"'s Room"),
category = guild.get_channel(660213767820410908),
user_limit = 99,
overwrites = {member: discord.PermissionOverwrite(manage_channels=True)}
)
await member.move_to(channel)
Finally please check that the app is in the server and has the Move Members permission on the server, otherwise member.move_to will throw a discord.Forbidden error.
来源:https://stackoverflow.com/questions/63136319/channel-set-permissions-and-move-to-channel