python - Softban command(discord.py)
问题描述
So im currently working on an softban command and i want that the user gets send a new inv link per dm. But it isnt sending them a message. Thats my code. Thank you!
@client.command(pass_context=True)
async def softban(ctx, user: discord.User=None):
if user == None:
embed = discord.Embed(title=f"**Softban**",
description=f"Softban will ban but immediately unban the user and send them a new invite link!"
f"\nUsage: {prefix}softban <User>")
await ctx.send(embed=embed)
try:
inv = await ctx.author.guild.create_invite(max_uses=1)
await user.send(f"You got softbanned from {ctx.author.guild}\nJoin again with this link: {inv}")
except:
await ctx.send("User probably has their dms closed!")
await ctx.guild.ban(user)
await asyncio.sleep(0.1)
await ctx.guild.unban(user)
await ctx.send(f"{user.mention} got softbanned!")
print(f"{user} got softbanned from {ctx.guild.name}")
解决方案
您的代码中有某种逻辑错误。你必须ban
和unban
用户在你的try
声明中。我也会考虑exceptions
再看一遍,看看你需要什么。
看看下面的代码:
@client.command(pass_context=True)
async def softban(ctx, user: discord.Member = None):
if user == None:
embed = discord.Embed(title=f"**Softban**",
description=f"Softban will ban but immediately unban the user and send them a new invite link!"
f"\nUsage: softban <User>")
await ctx.send(embed=embed)
try:
inv = await ctx.channel.create_invite(max_uses=1)
await user.send(f"You got softbanned from {ctx.author.guild}\nJoin again with this link: {inv}")
await ctx.guild.ban(user)
await asyncio.sleep(0.1)
await ctx.guild.unban(user)
await ctx.send(f"{user.mention} got softbanned!")
print(f"{user} got softbanned from {ctx.guild.name}")
except:
# Do whatever you want to if DMs are closed (Ban/Unban)
- 我们只能创建一个文本频道的邀请,所以我们使用
ctx.channel.create_invite
- 因为
user
我们说它必须是discord.Member
可以在此处找到相关文档:
推荐阅读
- javascript - Fullcalendar - 固定宽度事件:左对齐
- python - 如何仅获取父类对象的属性
- ios - 有没有办法从我的主类连接或访问我的“scrollView”到另一个类?
- java - OpenCV 4.1.0 Java:尝试将 BFMatcher 与基于 ORB 的关键点和描述符一起使用时如何修复“未知异常”?
- reactjs - TypeScript 和 React JS 访问 DOM 元素
- .net - 从 .net 客户端订阅 IBM MQ 主题
- c# - 如何获取有关找到该术语的字段的信息
- r - 将 na.rm 作为参数添加到 r 中的自定义函数
- javascript - 文本对齐 froalaEditor
- image - Github Markdown 中的图片缩略图