所以我正在开发一个机器人,我希望它拥有自己的票务支持系统。
我想做的是,在收到DM之后: -在“门票”类别中创建以DM作者(例如#David0001)命名的新频道 -设置该频道的权限,只有DM作者(和c的管理员)才能读取和写入该频道 -重新记录DM作者在其DM中写的内容
我当前正在使用最新的Async discord.py分支(我知道我可能应该使用重写功能,但是很好)
@client.event
async def on_message(message):
if message.server is None and message.author != client.user:
server = client.get_server("serverid")
for channel in server.channels:
if channel.name == str(message.author):
await client.send_message(message.author, "Hey you already have a support ticket open!")
break
else:
await client.create_channel(server, str(message.author), type=discord.ChannelType.text)
overwrite = discord.PermissionOverwrite()
overwrite.read_messages = True
overwrite.send_messages = True
overwrite.ban_members = False
for channel in server.channels:
if channel.name == str(message.author):
await client.edit_channel_permissions(channel.id, message.author, overwrite)
await client.send_message(channel.id, message.content)
break
else:
break
break
await client.process_commands(message)
我还希望它首先验证是否存在不存在具有用户名的支持频道,如果存在,则发送诸如“嘿,您已经打开了支持票频道”之类的小消息
此代码最初似乎起作用,但效果不佳,它确实在DM上创建了一个“ david0001”频道,但是它没有正确设置权限,也没有在预先存在的票证类别中进行设置(因为我不知道该怎么做),它不会重新转录用户在DM中写的任何内容,也不会验证用户是否拥有开放频道,只是不断创建一个新频道
答案 0 :(得分:1)
可以针对您的情况提出多种解决方案:
随着旧的异步分支的折旧,这是最新的代码。
我假设您使用的是齿轮系统:
这是为您的请求设计的命令,当用户使用它时,它会检查支持服务器,如果没有 Support 类别,它将创建一个,然后为用户创建频道。如果已经存在该要求,则只需将存储在request
中的消息发送到正确的通道即可。
import asyncio, discord
from discord.ext import commands
from discord.utils import get
class Cmd_support(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def support(self, ctx, *, request: str):
user = ctx.message.author
guild_id = 000000000000000000 # fill it up with your support guild id
support_server = self.client.get_guild(guild_id)
# try to match with a channel name
match = False
for channel in support_server.text_channels:
await asyncio.sleep(0)
if channel.name == user.name.lower(): # it's a match
match = True
user_support = get(support_server.text_channels, name = user.name.lower()) # get the user support channel as lower case, just like channels name
break
# end for
if not match: # if the channel doesn't exist, we create it
support_category_name = 'Support' # defines the Support category name, case sensitive
support_category = get(support_server.categories, name = support_category_name) # get the support category
user_support = get(support_server.text_channels, name = user.name.lower()) # get the user support channel as lower case, just like channels name
if support_category == None: # if the category is not found, we create it
# setting up the category permissions
support_category_permissions = {
support_server.default_role : discord.PermissionOverwrite(send_messages = False)
}
await support_server.create_category(name = support_category_name, overwrites = support_category_permissions)
support_category = get(support_server.categories, name = support_category_name) # redefine the variable with the new category
if user_support == None: # if the channel doesn't exist
# setting up the permissions for the channel
user_channel_permissions = {
support_server.default_role : discord.PermissionOverwrite(read_messages = False, send_messages = False), # othe users cannot see the channels
support_server.me : discord.PermissionOverwrite(read_messages = True, send_messages = True),
user : discord.PermissionOverwrite(read_messages = True, send_messages = True)
}
# now we create the channel
await support_server.create_text_channel(name = user.name, overwrites = user_channel_permissions, category = support_category)
user_support = get(support_server.text_channels, name = user.name.lower()) # redefine the support channel
# now send the message to the new channel
await user_support.send(request) # sends what the user sent to the command
def setup(client):
client.add_cog(Cmd_support(client))
它与命令解决方案略有不同,request
仅被message.content
代替,我们添加了条件:
请注意,无论消息内容如何,它都会创建一个频道并向其中发送消息。您可以使用以下条件对其进行过滤:
if message.content.startswith("something"):
if type(message.channel) == discord.DMchannel:
这是代码:
@commands.Cog.listener() # equivalent to discord.Event
async def on_message(self, message):
if type(message.channel) == discord.DMChannel:
user = message.author
guild_id = 000000000000000000 # fill it up with your support guild id
support_server = self.client.get_guild(guild_id)
# try to match with a channel name
match = False
for channel in support_server.text_channels:
await asyncio.sleep(0)
if channel.name == user.name.lower(): # it's a match
match = True
user_support = get(support_server.text_channels, name = user.name.lower()) # get the user support channel as lower case, just like channels name
break
# end for
if not match: # if the channel doesn't exist, we create it
support_category_name = 'Support' # defines the Support category name, case sensitive
support_category = get(support_server.categories, name = support_category_name) # get the support category
user_support = get(support_server.text_channels, name = user.name.lower()) # get the user support channel as lower case, just like channels name
if support_category == None: # if the category is not found, we create it
# setting up the category permissions
support_category_permissions = {
support_server.default_role : discord.PermissionOverwrite(send_messages = False)
}
await support_server.create_category(name = support_category_name, overwrites = support_category_permissions)
support_category = get(support_server.categories, name = support_category_name) # redefine the variable with the new category
if user_support == None: # if the channel doesn't exist
# setting up the permissions for the channel
user_channel_permissions = {
support_server.default_role : discord.PermissionOverwrite(read_messages = False, send_messages = False), # othe users cannot see the channels
support_server.me : discord.PermissionOverwrite(read_messages = True, send_messages = True),
user : discord.PermissionOverwrite(read_messages = True, send_messages = True)
}
# now we create the channel
await support_server.create_text_channel(name = user.name, overwrites = user_channel_permissions, category = support_category)
user_support = get(support_server.text_channels, name = user.name.lower()) # redefine the support channel
# now send the message to the new channel
await user_support.send(message.content) # sends what the user sent to the command
def setup(client):
client.add_cog(Event_on_message(client))
如您所见,它与我提出的第一个解决方案没有什么不同。
好吧,您确实应该将discord.py
版本更新到最新版本,旧的async
分支已不存在,并且已被他们正在使用的rewrite
版本所取代。
在您的终端中尝试以下小脚本:
>>> import discord
>>> discord.__version__
'1.2.3'
如果您的版本为 0.X.X ,则表示您不是最新版本!
考虑一下pip install discord.py --upgrade
以获得最新版本(比旧的异步版本好得多)
Usefull链接:discord.py - Migrating to v1.0(我已经在几个小时内迁移到了新版本,时间不算太长。您最好现在迁移,直到代码变得太大为止)
希望它有所帮助! 祝你有美好的一天!