使用 discord.py 重写创建即时邀请,似乎无法做到没有错误
Creating instant invite using discord.py rewrite, can't seem to do so without errors
我正在使用 python 让我的机器人生成即时邀请。
但是,我在查找和创建即时邀请时遇到了麻烦。
这是我的代码:
from progress.bar import Bar # Only to show a loading bar, all imports are successful.
print("Loading...")
imports = [
'import os',
'import sys',
'import asyncio',
'import discord',
'import random',
'import functools',
'import time as tm',
'from discord.ext import commands',
'from discord.ext.commands import when_mentioned'
]
bar = Bar('', max=len(imports))
for i in range(0, len(imports)):
exec(str(imports[i]))
bar.next()
bar.finish()
BOT_PREFIX = "!"
BOT_TOKEN = 'token'
OWNER_ID = int("owner's user id for support")
class Stuff(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def create_invite(self, ctx):
"""- Create instant invite"""
link = await discord.abc.GuildChannel.create_invite(self, max_age='300')
await ctx.send("Here is an instant invite to your server: "+link)
当命令在 discord 中为 运行 时,出现此错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Stuff' object has no attribute '_state'
如何正确生成即时邀请?
尝试使用 discord.TextChannel.create_invite()
像这样,
@commands.command()
async def create_invite(self, ctx):
"""Create instant invite"""
link = await ctx.channel.create_invite(max_age = 300)
await ctx.send("Here is an instant invite to your server: " + link)
这是一个例子;随意格式化:
@client.command(pass_context=True)
async def invite(ctx):
#Creating an invite link
link = await ctx.channel.create_invite(xkcd=True, max_age = 0, max_uses = 0)
#max_age = 0 The invite link will never exipre.
#max_uses = 0 Infinite users can join throught the link.
#-----------------------------------------------------#
#-------Embed Time-----#
em = discord.Embed(title=f"Join The {ctx.guild.name} Discord Server Now!", url=link, description=f"**{ctx.guild.member_count} Members** [**JOIN**]({link})\n\n**Invite link for {ctx.channel.mention} is created.**\nNumber of uses: **Infinite**\nLink Expiry Time: **Never**", color=0x303037)
#Embed Footer
em.set_footer(text=f"Made by </iFreakuYT>2.0#0908")
#Embed Thumbnail Image
em.set_thumbnail(url=ctx.guild.icon_url)
#Embed Author
em.set_author(name="INSTANT SERVER INVITE")
#-----------------------------------------#
await ctx.send(f"> {link}", embed=em)
输出:
我正在使用 python 让我的机器人生成即时邀请。 但是,我在查找和创建即时邀请时遇到了麻烦。 这是我的代码:
from progress.bar import Bar # Only to show a loading bar, all imports are successful.
print("Loading...")
imports = [
'import os',
'import sys',
'import asyncio',
'import discord',
'import random',
'import functools',
'import time as tm',
'from discord.ext import commands',
'from discord.ext.commands import when_mentioned'
]
bar = Bar('', max=len(imports))
for i in range(0, len(imports)):
exec(str(imports[i]))
bar.next()
bar.finish()
BOT_PREFIX = "!"
BOT_TOKEN = 'token'
OWNER_ID = int("owner's user id for support")
class Stuff(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def create_invite(self, ctx):
"""- Create instant invite"""
link = await discord.abc.GuildChannel.create_invite(self, max_age='300')
await ctx.send("Here is an instant invite to your server: "+link)
当命令在 discord 中为 运行 时,出现此错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Stuff' object has no attribute '_state'
如何正确生成即时邀请?
尝试使用 discord.TextChannel.create_invite()
像这样,
@commands.command()
async def create_invite(self, ctx):
"""Create instant invite"""
link = await ctx.channel.create_invite(max_age = 300)
await ctx.send("Here is an instant invite to your server: " + link)
这是一个例子;随意格式化:
@client.command(pass_context=True)
async def invite(ctx):
#Creating an invite link
link = await ctx.channel.create_invite(xkcd=True, max_age = 0, max_uses = 0)
#max_age = 0 The invite link will never exipre.
#max_uses = 0 Infinite users can join throught the link.
#-----------------------------------------------------#
#-------Embed Time-----#
em = discord.Embed(title=f"Join The {ctx.guild.name} Discord Server Now!", url=link, description=f"**{ctx.guild.member_count} Members** [**JOIN**]({link})\n\n**Invite link for {ctx.channel.mention} is created.**\nNumber of uses: **Infinite**\nLink Expiry Time: **Never**", color=0x303037)
#Embed Footer
em.set_footer(text=f"Made by </iFreakuYT>2.0#0908")
#Embed Thumbnail Image
em.set_thumbnail(url=ctx.guild.icon_url)
#Embed Author
em.set_author(name="INSTANT SERVER INVITE")
#-----------------------------------------#
await ctx.send(f"> {link}", embed=em)
输出: