Skip to content

Commit

Permalink
Merge pull request #5 from broemp/1-add-afk-command-to-discord-bot
Browse files Browse the repository at this point in the history
1 add afk command to discord bot
  • Loading branch information
broemp authored Aug 18, 2023
2 parents 248bc54 + bdeff3e commit 983eba0
Show file tree
Hide file tree
Showing 22 changed files with 274 additions and 106 deletions.
7 changes: 1 addition & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#API Settings
DISCORD_TOKEN=CHANGE_ME
TELEGRAM_TOKEN=CHANGE_ME

#DB Settings
DB_DRIVER=postgres
#DB Settings DB_DRIVER=postgres
DB_SOURCE=postgresql://{USER}:{PASSWORD}@{IP}:{PORT}/broempSignal?sslmode=disable

#Server Settings
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o /broempSignal main.go
RUN CGO_ENABLED=0 go build -o /broempSignal cmd/broempSignal.go

FROM alpine
WORKDIR /app
COPY --from=BUILD /broempSignal .
CMD ["/app/broempSignal"]
62 changes: 62 additions & 0 deletions api/afk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api

import (
"database/sql"
"net/http"

"github.com/gin-gonic/gin"
)

type afkRequest struct {
User int64 `json:"user" binding:"required"`
}

func (server *Server) createAFK(ctx *gin.Context) {
var req afkRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

afk, err := server.store.CreateAFK(ctx, sql.NullInt64{Int64: req.User, Valid: true})
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, afk)
}

type listAFKRequest struct {
User int64 `uri:"id" binding:"required"`
}

func (server *Server) listAFK(ctx *gin.Context) {
var req listAFKRequest
if err := ctx.ShouldBindUri(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}
afk, err := server.store.ListAFK(ctx, sql.NullInt64{Int64: req.User, Valid: true})
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, afk)
}

func (server *Server) countAFK(ctx *gin.Context) {
var req listAFKRequest
if err := ctx.ShouldBindUri(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}
afk, err := server.store.GetAFKCount(ctx, sql.NullInt64{Int64: req.User, Valid: true})
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, afk)
}
6 changes: 5 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func NewServer(store *db.Store) *Server {
router.POST("/users", server.createUser)
router.GET("/users/:id", server.getUser)
router.GET("/users/", server.listUser)
// router.GET("/users/discord/:discordid", server.getDiscordUser)

// AFK endpoint
router.POST("/afk/create", server.createAFK)
router.GET("/afk/list/:id", server.listAFK)
router.GET("/afk/count/:id", server.countAFK)

server.router = router
return server
Expand Down
1 change: 0 additions & 1 deletion main.go → cmd/broempSignal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func main() {

store := db.NewStore(conn)
server := api.NewServer(store)

err = server.Start(config.ServerAddress)
if err != nil {
log.Fatal("Cannot start server:", err)
Expand Down
3 changes: 2 additions & 1 deletion db/migration/000001_init_shema.down.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DROP TABLE IF EXISTS group;
DROP TABLE IF EXISTS afk;
DROP TABLE IF EXISTS hoepperCount;
DROP TABLE IF EXISTS invite;
DROP TABLE IF EXISTS group;
DROP TABLE IF EXISTS user;
29 changes: 19 additions & 10 deletions db/migration/000001_init_shema.up.sql
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
CREATE TABLE "user" (
"userid" BIGSERIAL PRIMARY KEY,
"discordid" bigint PRIMARY KEY,
"username" varchar NOT NULL,
"discordid" bigint NOT NULL,
"telegramid" bigint,
"created_at" timestamp DEFAULT (now())
);

CREATE TABLE "group" (
"groupid" BIGSERIAL PRIMARY KEY,
"hostid" bigserial NOT NULL,
"hostid" bigint NOT NULL,
"active" bool DEFAULT true,
"created_at" timestamp DEFAULT (now())
);

CREATE TABLE "invite" (
"groupid" bigserial,
"guestid" bigserial,
"guestid" bigint,
"accepted" bool DEFAULT null,
"created_at" timestamp DEFAULT (now()),
PRIMARY KEY ("groupid", "guestid")
);

CREATE TABLE "hoepperCount" (
"hoepper" bigserial,
"victim" bigserial,
"hoepper" bigint,
"victim" bigint,
"hoepperCount" integer,
PRIMARY KEY ("hoepper", "victim")
);

CREATE TABLE "afk" (
"afkid" BIGSERIAL PRIMARY KEY,
"userid" bigint,
"created_at" timestamp DEFAULT (now())
);

CREATE INDEX ON "user" ("discordid");

CREATE INDEX ON "group" ("groupid");
Expand All @@ -42,14 +47,18 @@ CREATE INDEX ON "hoepperCount" ("hoepper");

CREATE INDEX ON "hoepperCount" ("victim");

CREATE INDEX ON "afk" ("userid");

COMMENT ON COLUMN "invite"."accepted" IS 'null = waiting, false=declined, true=accepted';

ALTER TABLE "group" ADD FOREIGN KEY ("hostid") REFERENCES "user" ("userid");
ALTER TABLE "group" ADD FOREIGN KEY ("hostid") REFERENCES "user" ("discordid");

ALTER TABLE "invite" ADD FOREIGN KEY ("groupid") REFERENCES "group" ("groupid");

ALTER TABLE "invite" ADD FOREIGN KEY ("guestid") REFERENCES "user" ("userid");
ALTER TABLE "invite" ADD FOREIGN KEY ("guestid") REFERENCES "user" ("discordid");

ALTER TABLE "hoepperCount" ADD FOREIGN KEY ("hoepper") REFERENCES "user" ("discordid");

ALTER TABLE "hoepperCount" ADD FOREIGN KEY ("hoepper") REFERENCES "user" ("userid");
ALTER TABLE "hoepperCount" ADD FOREIGN KEY ("victim") REFERENCES "user" ("discordid");

ALTER TABLE "hoepperCount" ADD FOREIGN KEY ("victim") REFERENCES "user" ("userid");
ALTER TABLE "afk" ADD FOREIGN KEY ("userid") REFERENCES "user" ("discordid");
14 changes: 14 additions & 0 deletions db/query/afk.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- name: CreateAFK :one
INSERT INTO "afk" (
userid
) VALUES (
$1
) RETURNING *;

-- name: ListAFK :one
SELECT * from "afk"
WHERE userid = $1;

-- name: GetAFKCount :one
SELECT count(*) from "afk"
WHERE userid = $1;
12 changes: 4 additions & 8 deletions db/query/user.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
-- name: CreateUser :one
INSERT INTO "user" (
username, discordid, telegramid
discordid, username, telegramid
) VALUES (
$1, $2, $3
) RETURNING *;

-- name: GetUser :one
SELECT * from "user"
WHERE userid = $1;

-- name: GetUserByDiscordId :one
SELECT * FROM "user"
WHERE discordid = $1;

Expand All @@ -19,14 +15,14 @@ WHERE telegramid = $1;

-- name: DeleteUser :exec
DELETE FROM "user"
WHERE userid = $1;
WHERE discordid = $1;

-- name: UpdateTelegramId :exec
UPDATE "user" SET telegramid = $2
WHERE userid = $1;
WHERE discordid = $1;

-- name: ListUsers :many
SELECT * from "user"
ORDER BY userid
ORDER BY discordid
LIMIT $1
OFFSET $2;
50 changes: 50 additions & 0 deletions db/sqlc/afk.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions db/sqlc/afk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package db

import (
"context"
"database/sql"
"testing"

"github.com/stretchr/testify/require"
)

func TestCreateAFK(t *testing.T) {
user := createRandomUser(t)
afk, err := testQueries.CreateAFK(context.Background(), sql.NullInt64{Int64: user.Discordid, Valid: true})

require.NoError(t, err)
require.NotEmpty(t, afk)
}

func TestGetAFK(t *testing.T) {
user := createRandomUser(t)
i := sql.NullInt64{Int64: 0, Valid: true}
for i.Int64 <= 5 {
afk, err := testQueries.CreateAFK(context.Background(), sql.NullInt64{Int64: user.Discordid, Valid: true})
require.NoError(t, err)
require.NotEmpty(t, afk)
i.Int64++
}
num, err := testQueries.GetAFKCount(context.Background(), sql.NullInt64{Int64: user.Discordid, Valid: true})
require.NoError(t, err)
require.Equal(t, i.Int64, num)
}
28 changes: 28 additions & 0 deletions db/sqlc/group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package db

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestCreateGroup(t *testing.T) {
user := createRandomUser(t)

group, err := testQueries.CreateGroup(context.Background(), user.Discordid)
require.NoError(t, err)
require.NotEmpty(t, group)
}

func TestGetGroupByHost(t *testing.T) {
user := createRandomUser(t)

group, err := testQueries.CreateGroup(context.Background(), user.Discordid)
require.NoError(t, err)
require.NotEmpty(t, group)

group2, err := testQueries.GetGroupByHost(context.Background(), user.Discordid)
require.NoError(t, err)
require.Equal(t, group, group2)
}
9 changes: 7 additions & 2 deletions db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 983eba0

Please sign in to comment.