Python: How can I DM users that joins discord server using hikari not discord.py

Question:

I have a discord Bot that I need to send new users a DM once they join the server. I used on_member_join how ever it’s not working, I also tried many other ways to get it to work but it’s not working so I thought the problem is with discord.py and it’s dead…however the rest of the code works fine ..like when I type !ping it replies with pong
here’s the code

from discord.ext import commands
import discord, os, random, time

TOKEN = "mytoken"

client = commands.Bot(command_prefix="!")
prefix = "!"
bot = commands.Bot(prefix)

@client.event
async def on_read():
  print("Bot is ready!")

@client.command()
async def ping(ctx):
  await ctx.send("Pong")


@client.command()
async def flipcoin(ctx):
  heads_tails = ['Heads', 'Tails']
  choice = random.choice(heads_tails)
  await ctx.send(choice)

dmed_users = []


@client.event
async def on_member_join(member):
    if member not in dmed_users:
        message = "Welcome to the server!"
        embed = discord.Embed(title=message)
        await member.send(embed=embed)
        dmed_users.append(member)




client.run(TOKEN)

after editing the code with this

intents = discord.Intents.default()
intents.members = True

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

it gave the following error

    raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed

final edit

you can use this

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(client.run(TOKEN))
loop.run_until_complete(future)
loop.close()

or this

client.run(TOKEN)

Note very important what made it work !

I enabled member intents on the dashboard as well and that made it work!
the link bellow will help you enable them.

https://docs.discordbotstudio.org/setting-up-dbs/enabling-intents

Asked By: Mo .haytham

||

Answers:

you can use this

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(client.run(TOKEN))
loop.run_until_complete(future)
loop.close()

or this

client.run(TOKEN)

Very important note >> what made it work !

I enabled member intents on the dashboard as well and that made it work!
the link bellow will help you enable them.

https://docs.discordbotstudio.org/setting-up-dbs/enabling-intents

Answered By: Mo .haytham
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.