【问题标题】:discord has no attribute Intents once compiled一旦编译,discord 就没有 Intents 属性
【发布时间】:2022-11-23 02:38:11
【问题描述】:

所以我正在使用 Tkinter 制作一个 GUI,其中一个功能是它启动一个不和谐的机器人。 现在,当我在 VS Code 中运行代码时,一切正常。但是,当我使用 pyinstaller 编译它时,我收到一条错误消息,提示“Module discord has no attribute Intents”。 如果我将 bot 的代码放在一个单独的 python 文件中,并使用以下方法获取 tkinter 文件以加载 bot 文件:

os.popen('py botcode.py')

然后编译主要的 tkinter 文件,一切正常,但我希望机器人的代码与 tkinter 代码位于同一个文件中,而不是两个单独的文件中。 这是一些代码:

import tkinter as tk
import os, threading, json, collections
from tkinter import *
from tkinter import ttk
from tkinter import Scrollbar, messagebox
from threading import Thread
from PIL import ImageTk,Image

import discord
from discord.ext import commands, tasks
from itertools import cycle
import re
import subprocess, sys, random, smtplib, string, ctypes
import requests, asyncio, functools

def getintents():
    return discord.Intents().all()

token = "BOT TOKEN HERE"
client = commands.Bot(command_prefix=",", intents=getintents())
status = cycle(['Running Gremlins App', 'Coded by Gremlin',])
client.remove_command('help')

def RandomColor(): 
    randcolor = discord.Color(random.randint(0x000000, 0xFFFFFF))
    return randcolor

@client.event
async def on_ready():
    change_status.start()
    print('Online')

@tasks.loop(seconds=5)
async def change_status():
    await client.change_presence(activity=discord.Game(next(status)))

@client.command()
async def ping(ctx):
    embed = discord.Embed(description=f'Pong! {round(client.latency * 1000)}ms', color=RandomColor())
    await ctx.send(embed=embed)

class Main_Page(Temp):
    def __init__(self, parent, controller):
        Temp.__init__(self, parent)

        botbut = tk.Button(self, button_stylesG, text='Start Bot',command=lambda:startbot())
        botbut.pack()

        def startbot():
            def sbot():
                client.run(token)
            botstart = Thread(target=sbot)
            botstart.start()

为什么当我通过 VS Code 运行它时它可以工作,但在编译时却不能。 为什么当机器人代码在编译时位于单独的文件中但在同一文件中时却不起作用?...

【问题讨论】:

  • 始终将有问题的完整错误消息(从单词“Traceback”开始)(不在 cmets 中)作为文本(不是屏幕截图,不是指向外部门户的链接)。还有其他有用的信息。
  • 也许您有文件 discord.py 或文件夹 discord 并且 import 加载此文件/文件夹而不是模块 discord 并且它在您的文件中找不到 Intents。您必须重命名此文件/文件夹

标签: python tkinter discord discord.py


【解决方案1】:

那么你面临的问题可能是由于你的机器人结构

所以先去https://discord.com/developers/applications

选择你的机器人

单击选项按钮

然后转到bot向下滚动并启用所有意图

【讨论】:

  • 我在仪表板上启用了它们。如果我在不包含机器人代码的情况下编译 gui python,但让 gui 打开机器人代码所在的单独 python 文件,它工作正常。但是,如果我在编译的 gui 中包含代码,它会给我 intents 错误
【解决方案2】:

它应该是

return discord.Intents.all()

并不是

return discord.Intents().all()

或者你可以做

client = commands.Bot(command_prefix=",", intents=discord.Intents.all)

代替

def getintents():
    return discord.Intents.all()

client = commands.Bot(command_prefix=",", intents=getintents())

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2019-08-31
    • 2023-03-30
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多