这段代码工作得很好,但我重新安装了 py-cord 和不和谐,现在有些东西出现了一堆错误,我设法修复了大部分错误,但命令问题并没有真正起作用。
这是完整的代码:
from dotenv import load_dotenv
import discord
from discord.ext import commands
import os
from typing import Final
import traceback
from discord.ui import Button, View
whitelist = {
"mystic27" : 595287696000155729,
}
intents = discord.Intents.default()
intents.messages = True #NOQA
bot = commands.Bot(command_prefix='%', intents=intents)
@bot.event
async def on_ready():
print("Goldie is ready")
@bot.slash_command(name = "helpinvites", description = "More indepth help with Invite Commands")
async def helpinvite(ctx):
embed = discord.Embed(title="Invite Commands")
embed.add_field(name="Invites: ", value="> ```/invites``` -> Tracks all active invites the server has.\n> ```/deleteinv inv: Invite``` -> Deletes an invite.\n> ```/trackinvites``` -> Enables/disabled where or who a user was invited from.")
embed.add_field(name = "Logs: ",value="> ```/logjoin``` -> Logs user join in a certain channel.\n> ```/logleave``` -> Logs user leave in a certain channel.")
await ctx.respond(embed=embed)
@bot.slash_command(name = "helpmoderation",description = "More indepth help with Moderation commands")
async def helpmoderation(ctx):
embed = discord.Embed(title = "Moderation Commands")
embed.add_field(name = "Moderation: ",value="> ```/mute``` -> Mutes user.\n> ```/warn``` -> Warns user.\n> ```/ban``` -> Bans user.")
embed.add_field(name = "Chat moderation",value= "> ```/banword(s)``` -> Bans certain words.\n> ```/pg``` -> Goes completely PG (Reversible)")
await ctx.respond(embed=embed)
@bot.slash_command(name = "helputility",description = "Help with Utility commands")
async def helputil(ctx):
embed = discord.Embed(title="Utility Commands")
embed.add_field(name="Invites: ", value="> ```/invites``` -> Tracks all active invites the server has.\n> ```/deleteinv inv: Invite``` -> Deletes an invite.\n> ```/trackinvites``` -> Enables/disabled where or who a user was invited from.")
embed.add_field(name = "Moderation: ",value="> ```/mute``` -> Mutes user.\n> ```/warn``` -> Warns user.\n> ```/ban``` -> Bans user.\n>```/banword(s)``` -> Bans certain words.\n> ```/pg``` -> Goes completely PG (Reversible)")
button = Button(label = "Invite Help", style=discord.ButtonStyle.secondary,emoji="📩")
async def button_callback(self,button,interaction):
await interaction.response.edit_message(content = helpinvite(),view = self)
button.callback = button_callback
view = View()
view.add_item(button)
await ctx.respond(embed=embed,view=View)
@bot.slash_command(name = "logjoin", description = "Logs user join in a specified channel")
async def logjoin(ctx,channel):
global channellogkoin,logjoinaccess
channellogkoin = await bot.fetch_channel(channel)
embed = discord.Embed(title="Joins are enabled ✔!",description=f"Completed in {bot.latency}")
logjoinaccess = True
await ctx.respond(embed=embed)
@bot.event
async def on_member_join(member):
embed = discord.Embed(title=f"{member} joined. ✔",description=f"Completed in {bot.latency}")
if logjoinaccess == True:
await channellogkoin.send(embed=embed)
@bot.event
async def on_member_remove(member):
embed = discord.Embed(title=f"{member} left. ❌",description=f"Completed in {bot.latency}")
await channellogkoin.send(embed=embed)
@bot.slash_command(name = "ping",guild = 927168781275525210)
async def ping(ctx):
embed = discord.Embed(title="Pong!",description=f"Completed in {bot.latency}")
await ctx.respond(embed=embed)
@bot.slash_command(name="log",description = "Log a message in a channel")
async def log(ctx,channel,*contents: str):
channel = await bot.fetch_channel(channel)
await channel.send(f"```{contents} logged by <@{ctx.message.author.id}>```")
await ctx.respond("Command Completed")
@bot.slash_command(name="whois",description = "A short background check on a user.")
async def userinfo(ctx: commands.Context, user: discord.User):
user_id = user.id
username = user.name
await ctx.respond(f'User found: {user_id} -- {username}')
@bot.command()
async def updated(ctx):
await ctx.send("Goldie version 1.0")
@bot.slash_command(name = "menu", guild = 927168781275525210, description = "Menu for Goldie")
async def menu(ctx):
embed = discord.Embed(title = "Main Menu", description = "Welcome to Goldie!\nI am a Multi-Utility bot and can help with building your server and more!")
embed.add_field(name="Basic Utility: ", value=" > - ```/ping```")
embed.add_field(name = "Advanced Permissions: ", value="> - ```/goldaccess```")
embed.set_footer(text=f"Support Server - https://discord.gg/Goldie | Completed in {bot.latency}")
await ctx.respond(embed=embed)
load_dotenv()
TOKEN: Final[str] = os.getenv('discord_token')
def main() -> None:
bot.run(TOKEN)
if __name__ == '__main__':
main()
ps - 代码中提到的任何命令或网站都不起作用,并且添加是为了好玩。另外,我的命令“helpinvites”中的按钮不起作用,机器人加入事件/机器人删除事件也不起作用,因此也将有助于获得更多有关这些的帮助!
我尝试过寻找解决方案,查看其他人的问题并尝试重新安装。我不知道重新安装后发生了什么。
斜杠命令会弹出此错误:
AttributeError: 'Bot' object has no attribute 'slash_command'
机器人加入/删除事件没有错误,它根本不起作用,我会附加按钮错误,但我无法运行代码,因为命令不起作用。
查看文档,似乎您可以说:
@bot.command(description=“desc”)
def commandname(ctx):
# implementation
slash_command
可能不再受支持或其他什么。