Jadi ini kode saya, tetapi ketika saya menjalankan perintah saya mendapatkan kesalahan ini:

File "/Users/jbvann05/Documents/ColtsTARBot/bot.py", line 43, in test2
    channel = bot.get_channel(706573119325995109)
NameError: name 'guild' is not defined

Bagaimana cara memperbaikinya? Ada juga yang lebih ke kode seperti mengirim pesan tetapi tidak ingin saya menggunakan banyak kode

@bot.command(pass_context=True)
@commands.has_role(724348979533316115)
async def test2(ctx):
    server = ctx.message.guild
    channel = bot.get_channel(706573119325995109)
    member = server.get_member(449692646315786240)
    role = discord.utils.get(server.roles, name='Leg 1 Pitstop')
    member = server.get_member(643885922240430091)
    await member.add_roles(role)
0
jbvann05 20 November 2020, 20:16

1 menjawab

Jawaban Terbaik

Saya akan mencoba dan menggunakan fungsi fetch, ini adalah fungsi asinkron yang akan mengembalikan objek yang diinginkan.

@bot.command(pass_context=True)
@commands.has_role(724348979533316115)
async def test2(ctx):
    server = ctx.message.guild
    # Asynchronously fetch the text channel.
    channel = await bot.fetch_channel(706573119325995109)
    # Asynchronously fetch a member.
    member = await server.fetch_member(449692646315786240)
    role = discord.utils.get(server.roles, name='Leg 1 Pitstop')
    # Asynchronously fetch a member.
    member = await server.fetch_member(643885922240430091) 
    await member.add_roles(role)

Fungsi fetch_member: https://discordpy.readthedocs.io/en/latest/api.html?highlight=fetch%20member#discord.Guild.fetch_member
Fungsi fetch_channel: https://discordpy.readthedocs.io/en/latest/api.html?highlight=fetch%20channel#discord.Client.fetch_channel

0
Raz Kissos 21 November 2020, 18:06